我需要按照CORS
规范将文件上传到服务器javascript
。
互联网上有很多示例,实际上一切正常(文件已上传),但最后触发的事件是一个错误。
如果我查看目标,XMLHttpRequest
则以readyState=4
. 根据规范,这是
DONE: The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).
source http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-readystate
status
总是,0
但文件确实被上传,进度正常,服务器返回一个201 CREATED
.
一段代码:
var fd = new FormData(document.getElementById('viForm'));
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", submitAddress);
xhr.setRequestHeader('X-Auth-Token', token);
xhr.send(fd);
function uploadFailed(e) { console.log(e); }