gpt4 book ai didi

javascript - 来自 xmlhttprequest 的空响应文本

转载 作者:行者123 更新时间:2023-11-30 18:42:10 25 4
gpt4 key购买 nike

我正在使用 ajax 上传文件,为什么从 xmlhttprequest.responseText 返回的 responseText 为空?

我的代码:

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);

最佳答案

出于安全原因,不支持在 XMLHttpRequest 对象中上传文件

编辑:但是,使用 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'}));

关于javascript - 来自 xmlhttprequest 的空响应文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6558581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com