我有一个ajax调用将文件上传到服务器端。它似乎工作正常。
之间我在setInterval中再发送一个ajax调用来更新进度条宽度。我的期望是第一次调用将文件保存到服务器之间setInterval ajax调用将响应上传的字节到webUI。
setInterval ajax调用非常有效。但我无法得到该电话的回复。因为setInterval所有的ajax调用都会在第一次调用(FileUpload)完成后返回响应。
我在这里提到了我的代码。为此,我使用了MVC 4框架。
客户端代码:
//handleFileUpload() :: consolidate the files and send to server side..
function handleFileUpload(files) {
formData = new FormData();
$("#errorStatus").text('');
for (var i = 0; i < files.length; i++) {
filename = files[i].name;
filesize = files[i].size;
fileExtension = filename.split('.');
if ($.inArray(fileExtension[1], ['ppt', 'pptx', 'doc', 'docx','pdf'])==-1)
{
$("#errorStatus").text('We are not supporting this file types : ' +
files[i].name + '. \n Only support the following file extensions:
.ppt, .pptx, .doc, .docx, .pdf.');
return;
}
formData.append('file', files[i]);
}
formData.append('queueName', $(btnDDC).val());
setInterval('generateProgressBar()', 500);
sendFileToServer(formData);
}
//sendFileToServer() :: send job files to server side..
function sendFileToServer(formData) {
var uploadURL = "/JobSubmission/DragandDropPrintFromFile/";
jqXHR = $.ajax({
url: uploadURL,
type: "POST",
processData: false,
contentType: false,
data: formData,
success: function (data) {
//doing some success activities..
},
error: function (data) {
//doing some error activities..
}
});
}
generateProgressBar = function () {
$.ajax({
type: "get",
data: {},
url: "/jobsubmission/UploadProgressChanged/"),
success: function (data) {
var result = jquery.parsejson(data);
if (result != null && result != undefined) {
if (result.error == 0) {
var totalwidth = ((200 / result.countcheck) * result.count)+20;
var progresswidth = (result.count);
$(".progressbar div").css('width', totalwidth + 'px');
$(".sizeprog").html(progresswidth + ' mb');
}
}
},
error: function (data) {
console.log("couldn't reach the file..");
}
});
}