以下代码中的所有内容都将起作用,但它永远不会触发 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);
});
});
希望有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)