【问题标题】:How to get the current upload percentage using jquery + nodejs如何使用 jquery + nodejs 获取当前上传百分比
【发布时间】:2014-09-16 00:21:55
【问题描述】:
我正在尝试使用 jquery、ajax、express 和 nodejs 上传文件。在这里我想显示上传的进度。可能有插件或其他东西。无需发布该答案。我想研究这背后的技术。因此,如果您知道如何处理此问题,请帮助我。我有一个 ajax 调用来上传文件。
$.ajax({
url:'/controller/action',
//remaining parameters
});
【问题讨论】:
标签:
jquery
node.js
file-upload
express
progress-bar
【解决方案1】:
我找到了这段代码。我想它会对你有所帮助。你也可以使用这个插件JQuery File Upload
this.uploadFile = function(index) {
//baseClass == this
var file = baseClass.allFiles[index];
//Creating instance of FormData
var data = new FormData();
//Adding file
data.append('uploadFile', file.file);
//Sending it with ajax
$.ajax({
url: '/',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(response) {
var message = file.element.find('td.message');
if(response.status == 'ok') {
message.html(response.text);
file.element.find('button.uploadButton').remove();
}
else {
message.html(response.errors);
}
},
xhr: function() {
var xhr = $.ajaxSettings.xhr();
if ( xhr.upload ) {
console.log('xhr upload');
xhr.upload.onprogress = function(e) {
file.progressDone = e.position || e.loaded;
file.progressTotal = e.totalSize || e.total;
//updating downloading progress for the file
baseClass.updateFileProgress(index, file.progressDone, file.progressTotal, file.element);
//updating total progress
baseClass.totalProgressUpdated();
};
}
return xhr;
}
});
};