监听上传进度和完成事件
Js代码
xhr.upload.addEventListener("progress", function(e) {
if (e.lengthComputable) {
var percentage = Math.round((e.loaded * 100) / e.total);
img.style.opacity = 1-percentage/100.0;
}
}, false);
xhr.upload.addEventListener("load", function(e){
}, false); |
最后把数据模拟成multipart/form-data的格式上传
Js代码
xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary); // simulate a file MIME POST request.
xhr.setRequestHeader("Content-Length", fileSize);
var body = '';
body += "--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name=\""+dropbox.getAttribute('name')+"\"; filename=\"" + fileName + "\"\r\n";
body += "Content-Type: "+fileType+"\r\n\r\n";
body += fileData + "\r\n";
body += "--" + boundary + "--\r\n";
xhr.sendAsBinary(body); |
|