0
我正在建立一个文档文件上传。我想显示上传状态的百分比。进度模式上传节点js ajax
<div class="modal-body">
<div class="container_upload" id="new_file_form">
<div id="div_new_file_form" class="text-center" style="padding:2%;">
<div class="form-group text-center">
<input type="file" class="form-control" name="file_images"
id="url_file" multiple="true" accept=".pdf,.doc,.xls,.ppt,.docx,.xlsx,.pptx"/>
</div>
<button class="btn btn-success btn-lg" id="insert_btn">Carica</button>
<br>
</div>
</div>
</div>
我用fileinput.js到config我上传:
$("#url_file").fileinput({
language: 'it',
showUpload: false,
previewFileType:'any',
maxFileCount: max_n_files,
maxFileSize: maxsize/1000, //kB
browseClass: "btn btn-primary",
browseLabel: "Seleziona File",
browseIcon: "<i class=\"glyphicon glyphicon-file\"></i> ",
removeClass: "btn btn-danger",
removeLabel: "Elimina",
uploadClass: "btn btn-info",
uploadUrl: "http://localhost/upload",
allowedFileExtensions: ["pdf", "doc", "xls", "ppt", "docx", "xlsx", "pptx"]
});
而且我用一个AJAX调用的文件发送到该文件它处理我的节点服务器。
$.ajax({
beforeSend: showLoaderModal(),
url: '/uploadFile',
data: myFormData,
processData: false,
cache: false,
contentType: false,
type: 'POST',
xhr: function() {
var xhr = new XMLHttpRequest();
xhr.upload.onprogress = function(e) {
percentage = Math.floor(e.loaded/e.total *100) + '%';
//$(".progress-bar").append(percentage);
$(".progress-bar").empty();
$(".progress-bar").append(percentage);
$("progress-bar").attr("style","color: white;");
//console.log(percentage);
//console.log(percentage === "100%");
$(".progress-bar").animate({
width: percentage
},
function() {
upload_end = $(".progress-bar").css("width");
console.log(upload_end);
if(upload_end == "100%") {
console.log("ci siamo");
$(".end_upload").empty();
$(".end_upload").append('Upload completato. Conversione file in corso... ');
}
});
console.log(myFormData);
};
return xhr;
}
}).done(function (msg, state) {
hideLoaderModal();
success_msg(msg);
getMySize('size_disp');
$("#url_file").fileinput('clear');
if(msg.status == "DOCUMENT_UPLOADED"){
$("#upload_modal").modal('toggle');
//setTimeout(function(){ window.location.href ='/myFiles'; }, 2000);
}
$("#msg_booking").empty();
$("#msg_no_file").empty();
//call the getFiles function again for updating the list
if(window.location.pathname === '/book') {
get_myFiles(false);
}
else if(window.location.pathname.toLowerCase() === '/myfiles') {
getMyFiles();
}
}).fail(function (jqXHR, textStatus, errorThrown) {
hideLoader();
$("#upload_modal").modal('toggle');
$("#url_file").fileinput('clear');
getMySize('size_disp');
if(window.location.pathname === '/book') {
get_myFiles(false);
}
else if(window.location.pathname.toLowerCase() === '/myfiles') {
getMyFiles();
}
if(jqXHR.responseText) {
msg = JSON.parse(jqXHR.responseText);
error_msg(msg);
}
else {
error_msg(textStatus);
//alert("An error occurred:\n" + textStatus);
}
});
在我节点JS路线我把我的数据库文件的信息,如果一个文件不是为PDF格式,我会转换。 所以,我的问题是:我如何跟踪我的上传执行进度? 因为有xhr.upload.onpgrogress在Ajax调用之前结束。
啊进度条......整蛊私生子。我的建议只是基于转换文件所需的平均时间来伪造它,或者只是使用指示它正在处理它的微调器。 – Darkrum
您是否尝试过针对您的问题的解决方案? https://coligo.io/building-ajax-file-uploader-with-node/ –