在django中使用相同的输入名称上传多个文件 [英] multiple files upload using same input name in django

查看:129
本文介绍了在django中使用相同的输入名称上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < input type = file name =file > 
< input type = file name =file>
< input type = file name =file>

在django侧

  print request.FILES:

< MultiValueDict:{u'file':[
< TemporaryUploadedFile:captcha_bg.jpg(image / jpeg)>
< TemporaryUploadedFile:001_using_git_with_django.mov(video / quicktime)>
< TemporaryUploadedFile:ejabberd-ust.odt(application / vnd.oasis.opendocument.text)>
]}>

所以这三个文件都是单个request.FILES ['file']对象。如何处理从这里上传的每个文件?

解决方案

  FILES.getlist('file'):
#用文件做某事...



<编辑:我知道这是一个古老的答案,但我刚才发现,并且编辑了一个真正的答案。以前建议您可以直接在 request.FILES ['file'] 之间进行迭代。要访问MultiValueDict中的所有项目,请使用 .getlist('file')。只使用 ['file'] 只会返回找到该密钥的最后一个数据值。


i m having trouble in uploading multiple files with same input name:

<input type=file name="file">
<input type=file name="file">
<input type=file name="file">

at django side

print request.FILES :

<MultiValueDict: {u'file': [
<TemporaryUploadedFile: captcha_bg.jpg (image/jpeg)>,
<TemporaryUploadedFile: 001_using_git_with_django.mov (video/quicktime)>,
<TemporaryUploadedFile: ejabberd-ust.odt (application/vnd.oasis.opendocument.text)>
]}>

so all three files are under single request.FILES['file'] object . how do i handle for each files uploaded from here?

解决方案

for f in request.FILES.getlist('file'):
    # do something with the file f...

EDIT: I know this was an old answer, but I came across it just now and have edited the answer to actually be correct. It was previously suggesting that you could iterate directly over request.FILES['file']. To access all items in a MultiValueDict, you use .getlist('file'). Using just ['file'] will only return the last data value it finds for that key.

这篇关于在django中使用相同的输入名称上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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