Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert URL to File or Blob for FileReader.readAsDataURL

Reference: FileReader.readAsDataURL

Considering the following example:

function previewFile(file) {    var reader  = new FileReader();    reader.onloadend = function () {     console.log(reader.result);   }   reader.readAsDataURL(file); } 

It states:

instanceOfFileReader.readAsDataURL(blob);

blob: The Blob or File from which to read.

  1. How can a local file URL like: 'file:///C:/path-to/root.png' be passed to the readAsDataURL()

  2. Is FileReader() available in a Firefox Addon?

like image 948
erosman Avatar asked Jul 30 '14 20:07

erosman


People also ask

How do I get a Blob URL?

Right-click on the webpage and click “Inspect” in the menu. When the DevTools panel opens, click on the three vertical dots in the top-right corner and select “Undock into a separate window.” Press Ctrl + F on Windows or Cmd + F on Mac devices to find the blob URL. Enter “ blob:HTTP ” to find the link for the video.

What is readAsDataURL in JavaScript?

The readAsDataURL method is used to read the contents of the specified Blob or File . When the read operation is finished, the readyState becomes DONE , and the loadend is triggered. At that time, the result attribute contains the data as a data: URL representing the file's data as a base64 encoded string.

How do you convert a Blob URL to a audio file and save it to the server?

In the sendAudioFile function create a new FormData object. Append the Blob to the the formData. Now send the formData with the POST method to your server and use the body property for the formData . const sendAudioFile = file => { const formData = new FormData(); formData.


1 Answers

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(); 
like image 124
Felix Turner Avatar answered Oct 16 '22 21:10

Felix Turner