Django:在内存中打开上传的文件;在“表单清洁”方法中? [英] Django: Open uploaded file while still in memory; In the Form Clean method?

查看:104
本文介绍了Django:在内存中打开上传的文件;在“表单清洁”方法中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用表单清除方法验证上传的XML文件的内容,但无法打开文件进行验证。它以clean方法接缝,表明文件尚未从内存(或临时目录)移到目标目录。

I need to validate the contents of an uploaded XML file in my Form clean method, but I'm unable to open the file for validation. It seams, in the clean method, the file hasn't yet been moved from memory (or the temporary directory) to the destination directory.

例如,以下代码没有无效,因为文件尚未移动到该目的地。它仍在内存(或临时目录)中:

For example the following code doesn't work because the file hasn't been moved to that destination yet. It's still in memory (or the temporary directory):

xml_file = cleaned_data.get('xml_file')
xml_file_absolute = '%(1)s%(2)s' % {'1': settings.MEDIA_ROOT, '2': xml_file}
xml_size = str(os.path.getsize(xml_file_absolute))

当我查看 cleaned_data变量时,它显示为:

When I look at the "cleaned_data" variable it shows this:

{'xml_file': <InMemoryUploadedFile: texting.nzb (application/octet-stream)>}

cleaned_data.get('xml_file')仅返回 texting.nzb作为字符串。

cleaned_data.get('xml_file') only returns "texting.nzb" as a string.

还有另一种访问内存(或临时目录)中文件的方法吗?

Is there another way to access the the file in memory (or the temporary directory)?

同样,这是在表单的 clean 方法中,该方法与默认管理视图相关。一次又一次地告诉我,所有验证都应在表单而不是视图中进行。

Again, this is in my Form's clean method that's tied into the default administration view. I've been told time and time again that all validation should be handled in a Form, not the view. Correct?

推荐答案

我假设您已使用以下方式将表单绑定到文件:

I'm assuming that you've bound your form to the files using:

my_form = MyFormClass(request.POST, request.FILES)

如果有,一旦表单通过验证,就可以使用request.FILES字典访问文件内容本身:

If you have, once the form has been validated, you can access the file content itself using the request.FILES dictionary:

if my_form.is_valid():
    data = request.FILES['myfile'].read()

request.FILES ['myfile']对象是一个UploadedFile对象,因此它支持类似文件的读/写操作。

The request.FILES['myfile'] object is an UploadedFile object, so it supports file-like read/write operations.

如果您需要通过表单的 clean 方法(或清洁机器的任何方法)访问文件内容,则说明操作正确。 cleaned_data.get('xml_file')返回 UploadedFile 对象。该对象的 __ str __ 方法仅打印出字符串,这就是为什么只看到文件名的原因。但是,您可以访问全部内容:

If you need to access the file contents from within the form's clean method (or any method of the cleaning machinery), you are doing it right. cleaned_data.get('xml_file') returns an UploadedFile object. The __str__ method of that object just prints out the string, which is why you see only the file name. However, you can get access to the entire contents:

xml_file = myform.cleaned_data.get('xml_file')
print xml_file.read()

文档的这一部分有一些很好的例子: http://docs.djangoproject.com/en/dev/topics/http/file-上传/

This section of the docs has some great examples: http://docs.djangoproject.com/en/dev/topics/http/file-uploads/

这篇关于Django:在内存中打开上传的文件;在“表单清洁”方法中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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