我目前正在将一些上传表单构建到我的Python Flask网络应用中。我使用dropzone.js来拥有强大的拖放文件处理程序。
因此,上传后,文件将由某些Python函数处理,处理时间可能会很长。即使后台进程未完成,Dropzone.js的进度条也可以在0秒内从0传递到100。
我怎么有一个真正的进度栏?
这是我的JS代码:
Dropzone.options.myDropzone = {
url: "/upload",
method:'POST',
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 3,
maxFiles: 3,
addRemoveLinks: true,
acceptedFiles: 'application/pdf',
dictRemoveFile: 'Annuler l\'upload',
dictDefaultMessage: 'Selectionnez un document ou glissez le',
dictInvalidFileType: 'Ce type de format n\'est pas pris en compte',
init: function() {
let dzClosure = this; // Makes sure that 'this' is understood inside the functions below.
// for Dropzone to process the queue (instead of default form behavior):
document.getElementById("submit-all").addEventListener("click", function(e) {
// Make sure that the form isn't actually being sent.
e.preventDefault();
e.stopPropagation();
dzClosure.processQueue();
});
this.on("successmultiple", function(files, response) {
let topPx = 0;
for (let i = 0; i <= files.length; i++) {
if (i > 0) { topPx = 80 * i }
$("<div style='top: " + topPx + "px;' class='flash' onclick=\"$('.flash').fadeOut()\">Fichier(s) uploadé(s) avec succès : " + files[i]['name'] + "</div>").insertAfter($('.content'));
}
});
this.on("error", function(file, response) {
$("<div class='flash' onclick='$('.flash').fadeOut()'>Erreur lors de l'upload des fichiers : " + response + "</div>").insertAfter($('.content'));
});
}
};
这是Python的一小段代码:
@bp.route('/upload', methods=['GET', 'POST'])
@login_required
def upload():
if request.method == 'POST':
for file in request.files:
f = request.files[file]
f.save(os.path.join(current_app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
worker_from_python.main({
'path' : current_app.config['UPLOAD_FOLDER'],
'config': current_app.config['CONFIG_FILE']
})
os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'] + secure_filename(f.filename)))
return redirect('/upload')
return render_template('pdf/upload.html')
这是worker_from_python.main
调用,需要花费时间
预先感谢