如何在 Django 中获取 UploadedFile 的临时名称?
我正在进行一些文件验证,并希望将 UploadedFile 加载到位于“/tmp”目录中的外部库中,然后再将其保存到可以执行的位置。 Django 执行以下操作:
Django 会将上传的文件写入存储在系统临时目录中的临时文件。在类 Unix 平台上,这意味着您可以期望 Django 生成一个名为 /tmp/tmpzfp6I6.upload 的文件。
我希望能够获得“tmpzfp616.upload”。UploadedFile.name 给我“”,而 file.name 给我文件“example.mp3”的正确名称。
使用我正在使用的库,我需要将临时文件的文件路径传递给库,而不是文件本身,因此,需要字符串
吗?
提前感谢
:这是我的代码:
from django.core.files.uploadedfile import UploadedFile
class SongForm(forms.ModelForm):
def clean_audio_file(self):
file = self.cleaned_data.get('audio_file',False)
if file:
[...]
if file._size > 2.5*1024*1024:
try:
#The following two lines are where I'm having trouble, MP3 takes the path to file as input.
path = UploadedFile.temporary_file_path
audio = MP3('%s' %path)
except HeaderNotFoundError:
raise forms.ValidationError("Cannot read file")
else:
raise forms.ValidationError("Couldn't read uploaded file")
return file
使用“UploadedFile”我得到一个。 AttributeError“类型对象'UploadedFile'没有属性'temporary_file_path'”。如果我改用file.temporary_file_path(只是在黑暗中扔飞镖)我会得到一个IOError:
[Errno 2] 没有这样的文件或目录:'绑定方法 TemporaryUploadedFile.temporary_file_path of >'
我意识到 tempor_file_path 是我正在寻找的解决方案,我只是不知道如何使用它,并且文档和谷歌似乎在这个特定实例中都没有多大帮助。
I'm doing some file validation and want to load an UploadedFile into an external library while it is in the '/tmp' directory before I save it somewhere that it can be executed. Django does the following:
Django will write the uploaded file to a temporary file stored in your system's temporary directory. On a Unix-like platform this means you can expect Django to generate a file called something like /tmp/tmpzfp6I6.upload.
It ihe "tmpzfp616.upload' that I want to be able to get my hands on. UploadedFile.name gives me "" while file.name gives me the proper name of the file "example.mp3".
With the library I am using, I need to pass the filepath of the temporary file to the library, rather than the file itself and so, need the string.
Any ideas?
Thanks in advance.
EDIT: Here's my code:
from django.core.files.uploadedfile import UploadedFile
class SongForm(forms.ModelForm):
def clean_audio_file(self):
file = self.cleaned_data.get('audio_file',False)
if file:
[...]
if file._size > 2.5*1024*1024:
try:
#The following two lines are where I'm having trouble, MP3 takes the path to file as input.
path = UploadedFile.temporary_file_path
audio = MP3('%s' %path)
except HeaderNotFoundError:
raise forms.ValidationError("Cannot read file")
else:
raise forms.ValidationError("Couldn't read uploaded file")
return file
Using "UploadedFile" I get an AttributeError "type object 'UploadedFile' has no attribute 'temporary_file_path'". If I instead use file.temporary_file_path (just throwing darts in the dark here) I get an IOError:
[Errno 2] No such file or directory: 'bound method TemporaryUploadedFile.temporary_file_path of >'
I realize temporary_file_path is the solution I'm looking for, I just can't figure out how to use it and neither the docs nor google seem to be much help in this particular instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UploadedFile.temporary_file_path< /a>
UploadedFile.temporary_file_path