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

后端 未结 2 651
孤城傲影
孤城傲影 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 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

提交回复
热议问题