empty responseText from xmlhttprequest
I'm upload an file using ajax,why the responseText from xmlhttprequest.responseText is returned empty?
My code:
req = new XMLHttpRequest();
req.file = file;
req.addEventListener('change', changeProgress);
req.onreadystatechange = 开发者_如何学运维
function() {
if(this.readyState == 4) {
//etc..
alert(req.responseText);
}
};
req.open('POST','/upload',true);
req.send(file);
Uploading files in XMLHttpRequest object is not supported for security reasons
EDIT: This is, however, possible with XMLHttpRequest 2
function upload(blobOrFile) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/server', true);
xhr.onload = function(e) { ... };
// Listen to the upload progress.
var progressBar = document.querySelector('progress');
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
progressBar.value = (e.loaded / e.total) * 100;
progressBar.textContent = progressBar.value; // Fallback for unsupported browsers.
}
};
xhr.send(blobOrFile);
}
upload(new Blob(['hello world'], {type: 'text/plain'}));
精彩评论