python Django:在内存中打开上传的文件;在 Form Clean 方法中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/635524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django: Open uploaded file while still in memory; In the Form Clean method?
提问by Ty.
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.
我需要在我的 Form clean 方法中验证上传的 XML 文件的内容,但我无法打开文件进行验证。它接缝,在 clean 方法中,文件尚未从内存(或临时目录)移动到目标目录。
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))
When I look at the "cleaned_data" variable it shows this:
当我查看“cleaned_data”变量时,它显示了这一点:
{'xml_file': <InMemoryUploadedFile: texting.nzb (application/octet-stream)>}
cleaned_data.get('xml_file')
only returns "texting.nzb" as a string.
cleaned_data.get('xml_file')
仅以字符串形式返回“texting.nzb”。
Is there another way to access the the file in memory (or the temporary directory)?
有没有其他方法可以访问内存(或临时目录)中的文件?
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?
同样,这是在clean
绑定到默认管理视图的我的 Form方法中。我一次又一次地被告知所有验证都应该在表单中处理,而不是在视图中处理。正确的?
回答by Jarret Hardie
I'm assuming that you've bound your form to the files using:
我假设您已使用以下方法将表单绑定到文件:
my_form = MyFormClass(request.POST, request.FILES)
If you have, once the form has been validated, you can access the file content itself using the request.FILES dictionary:
如果你有,一旦表单被验证,你可以使用 request.FILES 字典访问文件内容本身:
if my_form.is_valid():
data = request.FILES['myfile'].read()
The request.FILES['myfile'] object is an UploadedFile object, so it supports file-like read/write operations.
request.FILES['myfile'] 对象是一个 UploadedFile 对象,因此它支持类似文件的读/写操作。
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 UploadedFileobject. 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:
如果您需要从表单的clean
方法(或任何清洁机器的方法)中访问文件内容,那么您就做对了。cleaned_data.get('xml_file')
返回一个UploadedFile对象。该__str__
对象的方法只是打印出字符串,这就是为什么您只能看到文件名的原因。但是,您可以访问全部内容:
xml_file = myform.cleaned_data.get('xml_file')
print xml_file.read()
This section of the docs has some great examples: http://docs.djangoproject.com/en/dev/topics/http/file-uploads/
文档的这一部分有一些很好的例子:http: //docs.djangoproject.com/en/dev/topics/http/file-uploads/