使用Flask上传文件时,应如何处理重复的文件名 [英] How should I handle duplicate filenames when uploading a file with Flask

查看:89
本文介绍了使用Flask上传文件时,应如何处理重复的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了这个问题,并回答了关于Flask的 https://stackoverflow.com/a/44926557/12322095 文件上传.

I recently came across this question and answer https://stackoverflow.com/a/44926557/12322095 regarding Flask file uploads.

这很好,直到我再次上传具有相同名称的图像.它没有更改图像或将其覆盖.

This worked perfectly fine until I uploaded an image with the same name again. It didn't change the image or overlayed it.

我的问题是,如果用户上传具有相同名称的图像,有什么方法可以显示错误消息,或者可以自动将名称更改为其他名称.

My question here is what if a user uploads an image with the same name, is there any way we could show an error message or maybe automatically change the name to something else.

我研究过要自动更改名称,可以通过 resolve_conflict 完成,但我不知道如何实现.

For automatic changing the name, I researched and it can be done via resolve_conflict but I couldn't understand how to implement it.

推荐答案

我的代码也作为参考

my code is ditto as the the reference

在保存文件之前,您需要创建某种uniuqe ID附加到文件名上.

You need to create some kind of uniuqe ID to append to the filename, before saving the file.

这可以通过以下方式完成:

This can be done with something like:

from uuid import uuid4
def make_unique(string):
    ident = uuid4().__str__()[:8]
    return f"{ident}-{string}"

可用于在字符串开头添加8个随机字符:

Which can be used to add 8 random characters to the start of a string:

>>> make_unique('something.txt')
'aa659bb8-something.txt'

要在上传代码中使用此文件,只需在保存之前通过该函数运行文件名即可.不过,请务必先通过 secure_filename 函数输入文件名:

To use this in the upload code, just run the filename through that function before you save. Be sure to put the filename through the secure_filename function first though:

        if file and allowed_file(file.filename):
            original_filename = secure_filename(file.filename)
            unique_filename = make_unique(original_filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], unique_filename))


尽管这样做是为了避免重复,但在较大的应用程序中,您可能希望扩展此方法.


Although this works for the purpose of avoiding duplicates, in a larger application you may wish to extend this approach.

如果将原始文件名 unique_filename 的值存储在数据库中,则可以在下载路径中执行以下操作:

If you store the values of original_filename and unique_filename in the database, then this allows you to do the following in the download route:

from flask import send_file
# ...
f = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
send_file(f, attachment_filename=original_filename)

这样做的好处是,文件使用唯一的标识符存储在服务器上,但是用户永远不会知道,因为文件是使用原始上载的文件名发回给他们的.

This has the advantage that the file is stored on your server with a unique identifier, but the user would never know this, as the file is served back to them with the originally uploaded filename.

实际上,您可能希望走得更远,只需使用完整的uuid字符串将文件保存到自己的末端即可;而不是使用上面的 make_unique 函数,将第三行更改为:

Infact you may wish to go further, and simply save the file on your end with a full uuid string; instead of using the make_unique function above, change that third line to:

unique_filename = uuid4().__str__()

由于 send_file 会根据提供的 attachment_filename 来猜测该mimetype,因此这仍将为文件提供正确的mimetype.

This will still serve the file with the correct mimetype, as send_file guesses the mimetype based on the provided attachment_filename.

这篇关于使用Flask上传文件时,应如何处理重复的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆