xhr send base64 string and decode it in the server to a file

前端 未结 1 635
无人及你
无人及你 2021-01-14 03:48


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.on         


        
相关标签:
1条回答
  • 2021-01-14 04:34
    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]);
    
    0 讨论(0)
提交回复
热议问题