Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Base64 String to javascript file object like as from file input form?

I want to convert Base64String extracted from file(ex: "AAAAA....~") to a javascript file object.

The javascript file object what I mean is like this code:

HTML:

<input type="file" id="selectFile" >  

JS:

$('#selectFile').on('change', function(e) {   var file = e.target.files[0];    console.log(file) } 

'file' variable is a javascript file object. So I want to convert a base64 string to the javascript file object like that.

I just want to get file object by decoding base64 string (encoded by other app from a file) without html file input form.

Thank you.

like image 703
Dayamre Avatar asked Mar 11 '16 12:03

Dayamre


People also ask

How do I get files from Base64?

How to convert Base64 to file. Paste your string in the “Base64” field. Press the “Decode Base64 to File” button. Click on the filename link to download the file.

How can I convert Base64 image to typescript?

Just create an image, set the src to the data URL, and use that image for your <image-cropper>. In any event, check my answer, should set you in the right direction if you absolutely most convert the data URL to something useful, such as a blob.

Can we convert Base64 to JSON?

Decode your Base64 data into JSON format. Input (Base64) - Paste your Base64 here. Upload Download Copy to Clipboard Conversion is Automatic. Output (JSON) - The converted JSON.

Can you Base64 encode a file?

Base64 is an encoding algorithm that converts any characters, binary data, and even images or sound files into a readable string, which can be saved or transported over the network without data loss. The characters generated from Base64 encoding consist of Latin letters, digits, plus, and slash.


1 Answers

Way 1: only works for dataURL, not for other types of url.

 function dataURLtoFile(dataurl, filename) {             var arr = dataurl.split(','),              mime = arr[0].match(/:(.*?);/)[1],              bstr = atob(arr[1]),               n = bstr.length,               u8arr = new Uint8Array(n);                        while(n--){              u8arr[n] = bstr.charCodeAt(n);          }                    return new File([u8arr], filename, {type:mime});      }            //Usage example:      var file = dataURLtoFile('data:text/plain;base64,aGVsbG8gd29ybGQ=','hello.txt');      console.log(file);

Way 2: works for any type of url, (http url, dataURL, blobURL, etc...)

 //return a promise that resolves with a File instance      function urltoFile(url, filename, mimeType){          return (fetch(url)              .then(function(res){return res.arrayBuffer();})              .then(function(buf){return new File([buf], filename,{type:mimeType});})          );      }            //Usage example:      urltoFile('data:text/plain;base64,aGVsbG8gd29ybGQ=', 'hello.txt','text/plain')      .then(function(file){ console.log(file);});
like image 127
cuixiping Avatar answered Oct 05 '22 13:10

cuixiping