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

前端 未结 1 688
无人及你
无人及你 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:[][;charset=][;base64],
    

    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)
提交回复
热议问题