如何在InMemoryUploadedFile django中上传文件

编程入门 行业动态 更新时间:2024-10-27 04:23:51
本文介绍了如何在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中上传文件

本文发布于:2023-10-29 04:16:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538700.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传文件   如何在   InMemoryUploadedFile   django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!