Python Bottle 多文件上传

标签 python bottle

我希望将多个文件上传到 Bottle 服务器。

单个文件上传效果很好,通过将 HTML 输入标签修改为“多个”,浏览按钮允许选择多个文件。上传请求处理程序只加载最后一个文件。如何一次性上传所有文件?

我正在试验的代码:

    from bottle import route, request, run

    import os

    @route('/fileselect')
    def fileselect():
        return '''
    <form action="/upload" method="post" enctype="multipart/form-data">
      Category:      <input type="text" name="category" />
      Select a file: <input type="file" name="upload" multiple />
      <input type="submit" value="Start upload" />
    </form>
        '''

    @route('/upload', method='POST')
    def do_upload():
        category   = request.forms.get('category')
        upload     = request.files.get('upload')
        print dir(upload)
        name, ext = os.path.splitext(upload.filename)
        if ext not in ('.png','.jpg','.jpeg'):
            return 'File extension not allowed.'

        #save_path = get_save_path_for_category(category)
        save_path = "/home/user/bottlefiles"
        upload.save(save_path) # appends upload.filename automatically
        return 'OK'

    run(host='localhost', port=8080)

最佳答案

mata's suggestion作品。您可以通过调用 getall() 来获取上传文件的列表。在 request.files 上。

@route('/upload', method='POST')
def do_upload():
    uploads = request.files.getall('upload')
    for upload in uploads:
        print upload.filename
    return "Found {0} files, did nothing.".format(len(uploads))

关于Python Bottle 多文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31642717/

相关文章:

python - 如何从 Python smtp 调试服务器捕获输出

python - Python中列表理解中的闭包

python - 在for语句中,我能够得到预期的结果。但是为什么我用while语句却得不到预期的结果呢?

python - 如何在 Bottle 框架中渲染阿拉伯字符串?

python - 类型错误 : 'dict' object is not callable when using request. json()

python - 'scrapy' 的网页抓取抓取了 0 个页面和项目

python - 批量加载和处理图像的最pythonic方式

python - request.headers 与 request.environ

python2.7 : Could not import get_url from bottle

Python tkinter 错误消息