一个输入中的Django多个文件不能被服务器读取 [英] Django multiple files in one input can't be read by server

查看:194
本文介绍了一个输入中的Django多个文件不能被服务器读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向Django提交一个多文件上传表单

 < input type =filename =files多个/> 

我在 request.FILES

 (MultiValueDict:{u'files':[(InMemoryUploadedFile:0202.jpg(image / jpeg)),( InMemoryUploadedFile:0203.jpg(image / jpeg))]})

但是我的混乱开始了。我以为 request.FILES ['files'] 会包含几个文件(看起来是一个列表),但它只显示

  0203.jpg 

InMemoryUploadedFile 部分,更重要的是:只有最后一个文件!

循环 request.FILES 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上,毕竟在这里稍有不同(客户端,至少)的问题: 在django中使用相同的输入名称上传多个文件



原来, 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多个文件不能被服务器读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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