django使用html5实现多文件选择批量文件上传

2023-05-16

在使用django过程中,上传文件功能总是一个一个文件上传,在文件数量多的时候用户体验不好,因此考虑能不能实现批量选择上传多文件,最终实现如下:
前端代码(html5支持多文件选择,核心是input的multiple=“true”):

<form method="post" action="/tests/UploadFile.php" id="myForm"
enctype="multipart/form-data" >
    <fieldset>
        <legend>Form Post Test</legend>
        <input name="myfiles" multiple="true"
        type="file" id="uploader" />
        <input type="submit" label="Submit" />
    </fieldset>
</form>

后端django部分代码(关键为request.FILES.getlist):
后端参考资料:https://www.jb51.net/article/163416.htm

files = request.FILES.getlist('myfiles')
for f in files:
   destination = open('d:/temp/' + f.name,'wb+')
   for chunk in f.chunks(): 
     destination.write(chunk)
   destination.close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

django使用html5实现多文件选择批量文件上传 的相关文章

随机推荐