如何使用bottle.py批量上传图像?

时间:2017-10-27 08:02:26

标签: python server bottle

将大量图像上传到服务器并将其保存在服务器上的最佳方法是什么?

我试图从表单中获取如下文件:

<form action="/upload" method="post">
    <div class="form-group">
        <label for="gallery">Select images:</label>
        <input id="gallery" type="file" name="gallery" accept=".gif,.jpg,.jpeg,.png" multiple>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

控制器如下所示:

@route('/upload', method='POST')
def newGallery():
    pictures = request.files.getall('gallery')
    for picture in pictures:
        picture.save("path/to/directory",overwrite=True)

return template('new.html', info)

但显然它不起作用,“request.files.getall('gallery')”返回null。

我也使用了“bottle.request.forms.getall('gallery')”但这个只返回文件名,而不是文件流。

对此有何看法?

1 个答案:

答案 0 :(得分:1)

对于我的个人瓶子项目,我在客户端使用了开源plupload JS库,并为瓶子做了一个小包装。

你可以安装它:

pip install plupload

然后使用它:

@post('/upload')
def index():
    from plupload import plupload
    return plupload.save(request.forms, request.files, os.getcwd())

有关更多示例,请查看https://github.com/JonathanHuot/bottlepy-plupload

相关问题