Convert URL To File Or Blob For FileReader.readAsDataURL
Answer :
To convert a URL to a Blob for FileReader.readAsDataURL() do this:
var request = new XMLHttpRequest();
request.open('GET', MY_URL, true);
request.responseType = 'blob';
request.onload = function() {
    var reader = new FileReader();
    reader.readAsDataURL(request.response);
    reader.onload =  function(e){
        console.log('DataURL:', e.target.result);
    };
};
request.send();
This information is outdated as of now, but cannot be deleted.
- You can create - Fileinstances just by specifying a path when your code is chrome-privileged:- new File("/path/to/file");- Fileis a sub-class of- Blob, so all- Fileinstances are also valid- Blobs.
 Please note that this requires a platform path, and not a file URL.
- Yes, - FileReaderis available to addons.
File and FileReader are available in all windows. If you want to use them in a non-window scope (like bootstrap.js or a code module), you may use nsIDOMFile/nsIDOMFileReader.
Expanding on Felix Turner s response, here is how I would use this approach with the fetch API. 
async function createFile(){
  let response = await fetch('http://127.0.0.1:8080/test.jpg');
  let data = await response.blob();
  let metadata = {
    type: 'image/jpeg'
  };
  let file = new File([data], "test.jpg", metadata);
  // ... do something with the file or return it
}
createFile();
Comments
Post a Comment