我建议在前端将传入文件拆分为较小的块。我这样做是为了在Flask应用程序中为大型文件上传实现暂停/恢复功能。
使用Sebastian Tschan的jquery插件,您可以通过
maxChunkSize在初始化插件时指定a来实现分块,如下所示:
$('#file-select').fileupload({ url: '/uploads/', sequentialUploads: true, done: function (e, data) { console.log("uploaded: " + data.files[0].name) }, maxChunkSize: 1000000 // 1 MB});
现在,客户端在上传大文件时将发送多个请求。您的服务器端代码可以使用
Content-Range标头将原始大文件重新修补在一起。对于Flask应用程序,视图可能类似于:
# Upload files@app.route('/uploads/', methods=['POST'])def results(): files = request.files # assuming only one file is passed in the request key = files.keys()[0] value = files[key] # this is a Werkzeug FileStorage object filename = value.filename if 'Content-Range' in request.headers: # extract starting byte from Content-Range header string range_str = request.headers['Content-Range'] start_bytes = int(range_str.split(' ')[1].split('-')[0]) # append chunk to the file on disk, or create new with open(filename, 'a') as f: f.seek(start_bytes) f.write(value.stream.read()) else: # this is not a chunked request, so just save the whole file value.save(filename) # send response with appropriate mime type header return jsonify({"name": value.filename, "size": os.path.getsize(filename), "url": 'uploads/' + value.filename, "thumbnail_url": None, "delete_url": None, "delete_type": None,})
对于您的特定应用程序,您只需确保仍随每个请求发送正确的auth标头。
希望这可以帮助!我在这个问题上挣扎了一段时间;)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)