上载多个图像

时间:2019-07-05 02:58:15

标签: python python-2.7 flask

我正在尝试在同一迭代中两次上传同一张图片。不知道为什么,但是使用下面的代码,我只得到了第一个save()的上载。第二个我在目录中仍然有文件,但是图像有0个字节。

path_for_images_large = os.path.join(current_app.static_folder, "uploads/gallery/"+str(company.id)+"/large/")
path_for_images_small = os.path.join(current_app.static_folder, "uploads/gallery/"+str(company.id)+"/small/")

if request.method == 'POST':
    for key, f in request.files.items():
        if key.startswith('file'):
            f.save(os.path.join(path_for_images_small, f.filename))
            f.save(os.path.join(path_for_images_large, f.filename))

1 个答案:

答案 0 :(得分:0)

第一次保存后,您位于文件的末尾,没有什么可写的,请键入f.seek(0)移至开头。

    f.save(os.path.join(path_for_images_small, f.filename))
    f.seek(0)    
    f.save(os.path.join(path_for_images_large, f.filename))