Flask中的大文件上传 [英] Large file upload in Flask
问题描述
我已经完成了服务器端处理函数,如下所示:
<$ p $方法= [POST,PUT])
def upload_process(文件名):
文件名(文件名)
fileFullPath = os.path.join(application.config ['UPLOAD_FOLDER'],filename)
with open(fileFullPath,wb)as f:
chunk_size = 4096
而真:
chunk = flask.request.stream.read(chunk_size)
如果len(chunk)== 0:
返回
f.write(chunk)
return jsonify({'filename':filename})
至于浏览器端,我应该给用户一个提交文件。一次一个文件。显示进度条以指示上传过程。
但我不知道浏览器端代码。如何使用JavaScript代码来启动上传并显示它的状态?
这将是一个艰巨的任务,你自己出来我会建议一个插件像 https://blueimp.github.io/jQuery-File-Upload/从这个项目的源代码中你可以看到他们使用一个方法名,它主要看文件的大小以及到目前为止已经传输了多少数据还有多少,以显示一个完整的百分比。
$ b $ (e,data){
var $ this = $(this);
$ this.find('。fileupload-progress')
.find('。progress')。progressbar(
'option',
'value',$ b $每个函数(){
$($。$ b $ .b).end()
.find('。progress-extended')。parseInt(data.loaded / data.total * 100,10) (this).html(
($ this.data('blueimp-fileupload')||
$ this.data('fileupload'))
._renderExtendedProgress(data)
);
});
https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery .fileupload-jquery-ui.js
所以,如果你想要自己的解决方案,我会建议你开始建立一个UI的div矩形有一个动态的宽度根据您的百分比计算更新的文件上传大小和数据上传...或只是去一个已经建立的解决方案。
I am attempting to implement a flask application for uploading files. This file could be very large. For example, almost 2G in size.
I have finished the server side process function like this:
@app.route("/upload/<filename>", methods=["POST", "PUT"])
def upload_process(filename):
filename = secure_filename(filename)
fileFullPath = os.path.join(application.config['UPLOAD_FOLDER'], filename)
with open(fileFullPath, "wb") as f:
chunk_size = 4096
while True:
chunk = flask.request.stream.read(chunk_size)
if len(chunk) == 0:
return
f.write(chunk)
return jsonify({'filename': filename})
As for browser side, I should give users a from to submit the file. One file at a time. Show progressbar to indicate the uploading process. But I have no idea about the browser side code. How can I use javascript code to start the uploading and show it status?
This will be a difficult task for your to figure out on your own. I would suggest a plugin like https://blueimp.github.io/jQuery-File-Upload/
You can see from this projects source code they use a method name which essentially looks at how large the file is and how much data has been transferred thus far and how much remains to show a percentage complete div.
code example from this project
progressall: function (e, data) {
var $this = $(this);
$this.find('.fileupload-progress')
.find('.progress').progressbar(
'option',
'value',
parseInt(data.loaded / data.total * 100, 10)
).end()
.find('.progress-extended').each(function () {
$(this).html(
($this.data('blueimp-fileupload') ||
$this.data('fileupload'))
._renderExtendedProgress(data)
);
});
}
https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery.fileupload-jquery-ui.js
So if you do want to come up with your own solution, I would suggest you start by building a UI div rectangle which has a dynamic width which updates according to your percentage calculation based upon the file upload size and data uploaded... or just go with an already established solution.
这篇关于Flask中的大文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!