xhr.send(file) doesn't post it as multipart

后端 未结 2 635
孤城傲影
孤城傲影 2021-01-07 11:22

On Firefox 3.6 and Chrome, using xhr.send(file) just puts the raw contents into the body of the request and it is not a true multipart/form-data upload.

Tried doing

相关标签:
2条回答
  • 2021-01-07 11:53

    The key thing is using sendAsBinary(body) istead of send(body). See the last comment on the page you linked!

    0 讨论(0)
  • 2021-01-07 12:11

    xhr.sendAsBinary() is non-standard. Instead, use xhr.send(FormData), which does create a multipart/form-data request, allows appending files, and arbitrary form data.

    var formData = new FormData();
    formData.append(file.name, file);
    
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/upload', true);
    xhr.onload = function(e) { ... };
    
    xhr.send(formData);  // multipart/form-data
    

    See http://www.html5rocks.com/en/tutorials/file/xhr2/#toc-send-formdata

    0 讨论(0)
提交回复
热议问题