Skip to content

How to get a File() or Blob() from an URL in javascript?

I try to upload an image to the Firebase storage from an URL (with ref().put(file))(

To do so i need a File or Blob, but whenever I try new File(url) it says “not enough arguments“…

EDIT: And I actually want to upload a whole directory of files, that’s why i can’t upload them via Console


Try using the fetch API. You can use it like so:

  .then(res => res.blob()) // Gets the response and returns it as a blob
  .then(blob => {
    // Here's where you get access to the blob
    // And you can use it for whatever you want
    // Like calling ref().put(blob)

    // Here, I use it to make an image appear on the page
    let objectURL = URL.createObjectURL(blob);
    let myImage = new Image();
    myImage.src = objectURL;
<div id="myImg"></div>

As of November 2020, the fetch API has about 96% browser support worldwide, with basically just IE missing it. You can get that to near 100% using a polyfill, which I recommend if you’re still targeting IE.