上传多张图片

2024-06-20 01:29:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图上传相同的图像在同一个迭代两次。不知道为什么,但是下面的代码只上传了第一次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))

Tags: path图像appforossavestaticcurrent
1条回答
网友
1楼 · 发布于 2024-06-20 01:29:21

在第一次保存之后,您就到了文件的末尾,没有什么要写的了,请键入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))

相关问题 更多 >