如何在InMemoryUploadedFile django中上传文件 [英] How to get the files uploaded in InMemoryUploadedFile django

查看:149
本文介绍了如何在InMemoryUploadedFile django中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在django 2.1中开发一个应用程序,在该应用程序中,我必须通过模态上传数量不确定的音频,然后将信息传递给启动模态的视图.但是,在填写主视图表单之前,不应将这些音频存储在数据库中.然后我想到了这些解决方案:

I am developing an application in django 2.1 in which I must upload an undetermined number of audios through a modal and then pass the information to the view from which the modal is launched. However, these audios should not be stored in the database until the main view form is filled out. Then I thought about these solutions:

首先,我想到了将其保存为会话属性,但是FileField的内容不是JSON可序列化的,这是行不通的.

First I thought about saving it as a session attribute but the contents of a FileField is not JSON serializable which did not work.

第二,我想到了LocalStorage属性,但是如果文件超过大小,我将遇到问题.

Second I thought about the LocalStorage property but if the files exceed the size I will have problems.

第三,我考虑过要获取文件路径,然后创建音频,但是由于我正在阅读,因此这是一种不好的做法,只有在磁盘上创建了文件(即,它在TemporaryUploadedFile中但我的文件中创建)时才能获取重量应少于1 MB

Third I thought about getting the file path and then create the audio but as I was reading is a bad practice and can only be obtained if the file is created on the disk, that is, if it is in TemporaryUploadedFile but my files should weigh less than one 1MB

我可以选择将所有小于2.5MB的文件都存储在InMemoryUploadedFile中,但是我不知道如何获取它们.有人知道这是怎么做的吗?或如何保存临时音频列表?

For which I have the option that all files that are loaded with a size smaller than 2.5MB are stored in InMemoryUploadedFile but I do not know how to obtain them. Does anyone know how this is done? or how else can I save a list of temporary audios?

推荐答案

InMemoryUploadedFile 是文件对象的包装器.您可以使用 file 属性访问文件对象.

InMemoryUploadedFile is a wrapper around a file object. You can access the file object using the file attribute.

file_in_memory # <InMemoryUploadedFile: xxx (xxx/xxx)>
file_object = file_in_memory.file

这篇关于如何在InMemoryUploadedFile django中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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