Django将文件上传到具体目录中,该目录取决于POST URI [英] Django upload file into specific directory that depends on the POST URI

查看:176
本文介绍了Django将文件上传到具体目录中,该目录取决于POST URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将上传的文件存储到根据POST请求的URI的特定目录中。也许,我也想将文件重命名为固定的东西(例如文件输入的名称),所以我有一个简单的方法来grep文件系统等,并且也避免可能的安全问题。

I'd like to store uploaded files into a specific directory that depends on the URI of the POST request. Perhaps, I'd also like to rename the file to something fixed (the name of the file input for example) so I have an easy way to grep the file system, etc. and also to avoid possible security problems.

在Django中,首选的方法是什么?

What's the preferred way to do this in Django?

编辑:我应该澄清一点,我有兴趣可能做这是一个文件上传处理程序,以避免将大文件写入文件系统两次。

I should clarify that I'd be interested in possibly doing this as a file upload handler to avoid writing a large file twice to the file system.

编辑2:我想可以将tmp文件'mv'到新位置。如果在同一个文件系统上,这是一个便宜的操作。

I suppose one can just 'mv' the tmp file to a new location. That's a cheap operation if on the same file system.

推荐答案

Django可以全面控制保存文件的位置(如果) 。请参见: http://docs.djangoproject.com/en/dev / topics / http / file-uploads /

Django gives you total control over where (and if) you save files. See: http://docs.djangoproject.com/en/dev/topics/http/file-uploads/

以下示例显示了如何组合URL和上传文件的名称,并将文件写入磁盘:

The below example shows how to combine the URL and the name of the uploaded file and write the file out to disk:

upload(request):
    folder = request.path.replace("/", "_")
    uploaded_filename = request.FILES['file'].name

    # create the folder if it doesn't exist.
    try:
        os.mkdir(os.path.join(BASE_PATH, folder))
    except:
        pass

    # save the uploaded file inside that folder.
    full_filename = os.path.join(BASE_PATH, folder, uploaded_filename)
    fout = open(full_filename, 'wb+')
    # Iterate through the chunks.
    for chunk in fout.chunks():
        fout.write(chunk)
    fout.close()

编辑:如何使用FileUploadHandler?它跟踪了代码,似乎你需要做四件事来重新调整TemporaryFileUploadHandler以保存在FILE_UPLOAD_TEMP_DIR之外:

How to do this with a FileUploadHandler? It traced down through the code and it seems like you you need to do four things to repurpose the TemporaryFileUploadHandler to save outside of FILE_UPLOAD_TEMP_DIR:


  1. 扩展 TemporaryUploadedFile 并覆盖 init ()将不同的目录传递给NamedTemporaryFile。它可以使用上面显示的try-mkdir-except-pass。

  1. extend TemporaryUploadedFile and override init() to pass through a different directory to NamedTemporaryFile. It can use the try-mkdir-except-pass I showed above.

扩展 TemporaryFileUploadHandler 并覆盖new_file()以使用上述类。

extend TemporaryFileUploadHandler and override new_file() to use the above class.

还可以扩展 init ()来接受您希望文件夹去的目录。

also extend init() to accept the directory where you want the folder to go.

动态添加请求处理程序,通过从URL确定的目录:

Dynamically add the request handler, passing through a directory determined from the URL:

request.upload_handlers = [ProgressBarUploadHandler(request.path.replace('/','_' )]

request.upload_handlers = [ProgressBarUploadHandler(request.path.replace('/', '_')]

虽然不重要,但是从头开始写一个处理程序还是比较容易:特别是,必须编写一行容易出错的缓冲读取,步骤3和4是必要的,因为FileUploadHandler在默认情况下不会传递请求信息,所以你必须分别告诉它是否要使用URL不知何故。

While non-trivial, it's still easier than writing a handler from scratch: In particular, you won't have to write a single line of error-prone buffered reading. Steps 3 and 4 are necessary because FileUploadHandlers are not passed request information by default, I believe, so you'll have to tell it separately if you want to use the URL somehow.

我不太会建议为此编写一个自定义FileUploadHandler。这真的是混合了责任层。相对于通过互联网上传文件的速度,执行本地文件副本是微不足道的。而如果这个文件很小,Django会将其保存在内存中,而不会将其写入临时文件。我有一个糟糕的感觉,你会得到所有这些工作,发现你甚至不能衡量性能差异。

I can't really recommend writing a custom FileUploadHandler for this. It's really mixing layers of responsibility. Relative to the speed of uploading a file over the internet, doing a local file copy is insignificant. And if the file's small, Django will just keep it in memory without writing it out to a temp file. I have a bad feeling that you'll get all this working and find you can't even measure the performance difference.

这篇关于Django将文件上传到具体目录中,该目录取决于POST URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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