问题描述
当我向Django提交一个多文件上传表单
code>和 .iteritems()也只能覆盖那个文件, request.FILES ['files'] 显然是不可战胜的;下面的代码实际上冻结了控制台,并使其嗡嗡声:
for request.FILES ['files']:
print v
print type(v)
这正常吗?我在做什么错了?
当我开始认为这可能是一个错误:我在Windows 7上使用Django 1.4.2与Python 2.7。 / p>解决方案
当我向Django提交一个多文件上传表单
code>和 .iteritems()也只能覆盖那个文件, request.FILES ['files'] 显然是不可战胜的;下面的代码实际上冻结了控制台,并使其嗡嗡声:
for request.FILES ['files']:
print v
print type(v)
这正常吗?我在做什么错了?
当我开始认为这可能是一个错误:我在Windows 7上使用Django 1.4.2与Python 2.7。 / p>解决方案
事实证明,答案在Stack Overflow上,毕竟在这里稍有不同(客户端,至少)的问题:
原来, request.FILES ['files'] 是错的,应该是 request.FILES.getlist('file')。
为什么这种情况对我来说是一个完整的谜。如果任何人都可以回答一个解释,我会很乐意切换接受的答案。
When I submit to Django a multiple files upload form
<input type="file" name="files" multiple />
I get a sensible result in request.FILES:
(MultiValueDict: {u'files': [(InMemoryUploadedFile: 0202.jpg (image/jpeg)), (InMemoryUploadedFile: 0203.jpg (image/jpeg))]})
But then my confusion starts. I thought request.FILES['files'] would contain a couple of files (appears to be a list), but it shows only
0203.jpg
No InMemoryUploadedFile part and more importantly: only the last file!
Looping through request.FILES with .iteritems() only goes over that one file too, request.FILES['files'] is distinctly uniterable; the below code actually froze up the console and made it beep endlessly:
for v in request.FILES['files']: print v print type(v)
So... Is any of this this normal? What am I doing wrong?
As I'm starting to think this may be a bug: I'm using Django 1.4.2 with Python 2.7 on Windows 7.
It turns out the answer was on Stack Overflow after all, on a slightly different (client side, at least) problem here: multiple files upload using same input name in django
Turns out that request.FILES['files'] was wrong and should have been request.FILES.getlist('file').
Why this is the case is a complete mystery to me. If anyone can answer with an explanation, I'll gladly switch the accepted answer.
这篇关于一个输入中的Django多个文件不能被服务器读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!