Python Bottle - 如何上传媒体文件而不导致服务器崩溃

1 投票
2 回答
2103 浏览
提问于 2025-04-18 01:10

我在使用这个问题中的答案时,看到了一条评论:

   raw = data.file.read() # This is dangerous for big files

我想知道如何在不这样做的情况下上传文件?我到目前为止的代码是:

@bottle.route('/uploadLO', method='POST')
def upload_lo():
    upload_dir = get_upload_dir_path()
    files = bottle.request.files
    print files, type(files)
    if(files is not None):
        file = files.file
        print file.filename, type(file)
        target_path = get_next_file_name(os.path.join(upload_dir, file.filename))
        print target_path
        shutil.copy2(file.read(), target_path)  #does not work. Tried it as a replacement for php's move_uploaded_file
    return None

这段代码的输出是:

127.0.0.1 - - [03/Apr/2014 09:29:37] "POST /uploadLO HTTP/1.1" 500 1418
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\bottle.py", line 862, in _handle
    return route.call(**args)
  File "C:\Python27\lib\site-packages\bottle.py", line 1727, in wrapper
    rv = callback(*a, **ka)
  File "C:\dev\project\src\mappings.py", line 83, in upload_lo
    shutil.copy2(file.read(), target_path)
AttributeError: 'FileUpload' object has no attribute 'read'

我正在使用的是python bottle v.12,dropzone.min.js和mongodb。我还在参考这个教程:

http://www.startutorial.com/articles/view/how-to-build-a-file-upload-form-using-dropzonejs-and-php

2 个回答

1

为了补充ron.rothman的精彩回答...要解决你的错误信息,可以试试这个

@bottle.route('/uploadLO', method='POST')
def upload_lo():
    upload_dir = get_upload_dir_path()
    files = bottle.request.files

    # add this line
    data = request.files.data

    print files, type(files)

    if(files is not None):
        file = files.file
        print file.filename, type(file)
        target_path = get_next_file_name(os.path.join(upload_dir, file.filename))
        print target_path

        # add Ron.Rothman's code
        data_blocks = []
        buf = data.file.read(8192)
        while buf:
            data_blocks.append(buf)
            buf = data.file.read(8192)

        my_file_data = ''.join(data_blocks)
        # do something with the file data, like write it to target
        file(target_path,'wb').write(my_file_data)

    return None
3

这被称为“文件吸取”:

raw = data.file.read() 

而且你不想这样做(至少在这种情况下不想)。

这里有一种更好的方法来读取一个大小未知(可能很大的)二进制文件:

data_blocks = []

buf = data.file.read(8192)
while buf:
    data_blocks.append(buf)
    buf = data.file.read(8192)

data = ''.join(data_blocks)

你可能还想在累积的大小超过某个阈值时停止迭代。

希望这对你有帮助!


第二部分

你问到了限制文件大小,所以这里有一个修改过的版本来实现这个功能:

MAX_SIZE = 10 * 1024 * 1024 # 10MB
BUF_SIZE = 8192

# code assumes that MAX_SIZE >= BUF_SIZE

data_blocks = []
byte_count = 0

buf = f.read(BUF_SIZE)
while buf:
    byte_count += len(buf)

    if byte_count > MAX_SIZE:
        # if you want to just truncate at (approximately) MAX_SIZE bytes:
        break
        # or, if you want to abort the call
        raise bottle.HTTPError(413, 'Request entity too large (max: {} bytes)'.format(MAX_SIZE))

    data_blocks.append(buf)
    buf = f.read(BUF_SIZE)

data = ''.join(data_blocks)

这不是完美的,但它简单,而且在我看来已经足够好了。

撰写回答