gpt4 book ai didi

javascript - xhr.upload.onprogress 不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 04:04:35 26 4
gpt4 key购买 nike

除了永远不会触发 xhr.upload.onprogress 事件之外,以下代码中的所有内容都可以正常工作。

$(function(){

var xhr;

$("#submit").click(function(){
var formData = new FormData();
formData.append("myFile", document.getElementById("myFileField").files[0]);
xhr = new XMLHttpRequest();
xhr.open("POST", "./test.php", true);
xhr.send(formData);

xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200){
console.log(xhr.responseText);
}
}

xhr.upload.onprogress = function(e) {
// it will never come inside here
}
});
});

最佳答案

您应该在打开连接之前创建监听器,如下所示:

$(function(){

var xhr;

$("#submit").click(function(){
var formData = new FormData();
formData.append("myFile", document.getElementById("myFileField").files[0]);
xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200){
console.log(xhr.responseText);
}
}

xhr.upload.onprogress = function(e) {
// it will never come inside here
}

xhr.open("POST", "./test.php", true);
xhr.send(formData);
});
});

希望对您有所帮助。

关于javascript - xhr.upload.onprogress 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14160308/

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