Skip to content Skip to sidebar Skip to footer

Xhr Send Base64 String And Decode It In The Server To A File

I am trying to to send a base64 encoded img to server,the javascript looks like var xhr=new XMLHttpRequest() var reader=new FileReader() reader.onloadend=function(e){ xhr.onload=f

Solution 1:

reader.readAsDataURL(file)

A data URL is NOT the same as a base64 version of the file. You get extra garbage in it. It looks like this:

data:[<MIME-type>][;charset=<encoding>][;base64],<data>

See Wikipedia.

Try doing a simple regex on it:

var data = dataURL.match(/,(.*)$/)[1];

Or, in your code,

xhr.send(e.target.result.match(/,(.*)$/)[1]);

Post a Comment for "Xhr Send Base64 String And Decode It In The Server To A File"