当前位置: 首页 > 知识库问答 >
问题:

用Flask处理大文件上传

董胡媚
2023-03-14

用Flask处理非常大的文件上传(1 GB+)的最佳方式是什么?

我的应用程序基本上接受多个文件,为它们分配一个唯一的文件号,然后根据用户选择的位置将其保存在服务器上。

我们如何运行文件上传作为一个后台任务,这样用户就不会有浏览器旋转1小时,而是可以立即进入下一页?

    null

共有1个答案

缑兴贤
2023-03-14

我认为超级简单的方法就是把文件分成许多小的部分/块发送。因此,将有两个部分的工作,前端(网站)和后端(服务器)。对于前端部分,您可以使用dropzone.js这样的东西,它不包含额外的依赖项和合适的CSS。您所要做的就是将类dropzone添加到窗体中,它会自动将其转换为一个特殊的拖放字段(您也可以单击并选择)。

但是,默认情况下,dropzone不会对文件进行分块处理。幸运的是,它真的很容易启用。下面是启用DropzoneJS分块的示例文件上传表单:

<html lang="en">
<head>

    <meta charset="UTF-8">

    <link rel="stylesheet" 
     href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/min/dropzone.min.css"/>

    <link rel="stylesheet" 
     href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/min/basic.min.css"/>

    <script type="application/javascript" 
     src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/min/dropzone.min.js">
    </script>

    <title>File Dropper</title>
</head>
<body>

<form method="POST" action='/upload' class="dropzone dz-clickable" 
      id="dropper" enctype="multipart/form-data">
</form>

<script type="application/javascript">
    Dropzone.options.dropper = {
        paramName: 'file',
        chunking: true,
        forceChunking: true,
        url: '/upload',
        maxFilesize: 1025, // megabytes
        chunkSize: 1000000 // bytes
    }
</script>
</body>
</html>

下面是使用烧瓶的后端部分:

import logging
import os

from flask import render_template, Blueprint, request, make_response
from werkzeug.utils import secure_filename

from pydrop.config import config

blueprint = Blueprint('templated', __name__, template_folder='templates')

log = logging.getLogger('pydrop')


@blueprint.route('/')
@blueprint.route('/index')
def index():
    # Route to serve the upload form
    return render_template('index.html',
                           page_name='Main',
                           project_name="pydrop")


@blueprint.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']

    save_path = os.path.join(config.data_dir, secure_filename(file.filename))
    current_chunk = int(request.form['dzchunkindex'])

    # If the file already exists it's ok if we are appending to it,
    # but not if it's new file that would overwrite the existing one
    if os.path.exists(save_path) and current_chunk == 0:
        # 400 and 500s will tell dropzone that an error occurred and show an error
        return make_response(('File already exists', 400))

    try:
        with open(save_path, 'ab') as f:
            f.seek(int(request.form['dzchunkbyteoffset']))
            f.write(file.stream.read())
    except OSError:
        # log.exception will include the traceback so we can see what's wrong 
        log.exception('Could not write to file')
        return make_response(("Not sure why,"
                              " but we couldn't write the file to disk", 500))

    total_chunks = int(request.form['dztotalchunkcount'])

    if current_chunk + 1 == total_chunks:
        # This was the last chunk, the file should be complete and the size we expect
        if os.path.getsize(save_path) != int(request.form['dztotalfilesize']):
            log.error(f"File {file.filename} was completed, "
                      f"but has a size mismatch."
                      f"Was {os.path.getsize(save_path)} but we"
                      f" expected {request.form['dztotalfilesize']} ")
            return make_response(('Size mismatch', 500))
        else:
            log.info(f'File {file.filename} has been uploaded successfully')
    else:
        log.debug(f'Chunk {current_chunk + 1} of {total_chunks} '
                  f'for file {file.filename} complete')

    return make_response(("Chunk upload successful", 200))
 类似资料:
  • 问题内容: 用Flask处理超大文件上传(1 GB以上)的最佳方法是什么? 我的应用程序实际上需要多个文件,为它们分配一个唯一的文件号,然后根据用户选择的位置将其保存在服务器上。 我们如何将文件上传作为后台任务运行,以使用户在1小时内没有浏览器旋转,而是可以立即进入下一页? Flask开发服务器能够处理大量文件(50gb需要1.5个小时,上传速度很快,但将文件写入空白文件的速度却很慢) 如果我用T

  • 本文向大家介绍PHP文件上传问题汇总(文件大小检测、大文件上传处理),包括了PHP文件上传问题汇总(文件大小检测、大文件上传处理)的使用技巧和注意事项,需要的朋友参考一下 由于涉及到本地和服务器两方面的安全问题,所以基于input type="file"形式的页面文件上传一直处于一个很尴尬的位置。一方面,用户不希望隐私泄露,所以浏览器无法对用户在上传时选择的文件做有效的判 断。另一方面,为了服务器

  • 你想处理一个由用户上传的文件,比如你正在建设一个类似Instagram的网站,你需要存储用户拍摄的照片。这种需求该如何实现呢? 要使表单能够上传文件,首先第一步就是要添加form的enctype属性,enctype属性有如下三种情况: application/x-www-form-urlencoded 表示在发送前编码所有字符(默认) multipart/form-data 不对字符

  • 问题内容: 我正在考虑将WCF用于一个项目,该项目需要人们能够将大文件(64MB-1GB)上传到我的服务器。我将如何使用WCF处理此问题,可能还具有恢复上传的功能。 为了处理更大的客户群,我想通过WCF测试JSON。这将如何影响文件上传?可以通过JSON完成,还是需要将上传部分切换到REST? 问题答案: 如果要上传大文件,则肯定需要查看WCF流模式。 基本上,您可以更改绑定上的传输模式;默认情况

  • 问题内容: 我最近刚开始玩go,所以我还是菜鸟,对不起,如果我犯了太多错误。我已经尝试解决了很长时间,但我只是不明白发生了什么。在我的main.go文件中,我有一个主要功能: 处理程序函数如下所示: 我知道这不是服务网站的最佳方法提交功能如下所示: 问题是,当执行Submit 函数时,它是并且是一个空字符串,如果r.FormFile返回以下错误,它将继续执行直到第一个: 我不明白为什么r.Meth

  • 问题内容: 我正在尝试处理较大的(可能多达200M)JSON文件。文件的结构基本上是对象数组。 因此,遵循以下原则: 每个对象都具有任意属性,不必与数组中的其他对象共享它们(例如,具有相同的属性)。 我想对数组中的每个对象进行处理,并且由于文件可能很大,因此无法将整个文件内容都包含在内存中,无法解码JSON并遍历PHP数组。 因此,理想情况下,我想读取文件,为每个对象获取足够的信息并进行处理。如果