我正在尝试使用jquery,ajax,express和nodejs上传文件。在这里,我想展示上传的进度。可能有一个插件或其他东西。无需发布答案。我想研究背后的技术。如果您知道如何处理,请帮助我。我有一个ajax调用来上传文件。
$.ajax({
url:'/controller/action',
//remaining parameters
});
答案 0 :(得分: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;
}
});
};
答案 1 :(得分:-4)
您可以使用Multer上传文件: Multer