gpt4 book ai didi

php - 如何在ajax文件上传中显示进度条

转载 作者:可可西里 更新时间:2023-10-31 23:22:20 24 4
gpt4 key购买 nike

我的代码发布了 ajax 请求,但没有显示进度条。请帮助更正代码以显示工作进度条。

$(document).ready(function () {
$("#uploadbutton").click(function () {
var filename = $("#file_path$i").val(); //get form data;

$.ajax({
type: "html",
url: "share.php",//onwhich post ajax data;
enctype: 'multipart/form-data',
data: {
file: filename
},
success: function () {
alert("Data Uploaded: ");
}
});
});
});

最佳答案

@whitneyit : 我们可以使用 xhr: ajax 的函数来跟踪文件上传,就像这样

$.ajax({
url: 'handler/FileUploader.ashx?FileName=' + file.name, //server script to process data
type: 'POST',
xhr: function () {
myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
}
return myXhr;
},
success: function (result) {
//On success if you want to perform some tasks.
},
data: file,
cache: false,
contentType: false,
processData: false
});
function progressHandlingFunction(e) {
if (e.lengthComputable) {
var s = parseInt((e.loaded / e.total) * 100);
$("#progress" + currFile).text(s + "%");
$("#progbarWidth" + currFile).width(s + "%");
if (s == 100) {
triggerNextFileUpload();
}
}
}

关于php - 如何在ajax文件上传中显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14806542/

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