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

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

问题描述

我无法上传具有相同输入名称的多个文件:

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">

在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'] object 下.我如何处理从这里上传的每个文件?

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...

我知道这是一个旧答案,但我刚刚遇到它并已将答案编辑为实际上是正确的.之前建议您可以直接迭代 request.FILES['file'].要访问 MultiValueDict 中的所有项目,请使用 .getlist('file').仅使用 ['file'] 只会返回它为该键找到的最后一个数据值.

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天全站免登陆