django文件批量上传-简写版

时间:2022-01-26 15:18:36

模板中创建表单

<form method='post' enctype='multipart/form-data' action='/upload/'>
<input type='file' name='upload_img'/>
<input type='file' name='upload_img'/>
<input type='submit' value='submit'>
</form>

在urls中到处一个upload地址

 url(r'^upload/$',upload),

最后写控制器

@csrf_exempt
def upload(req):
if req.method=='POST':
files=req.FILES.getlist('upload_img')
for f in files:
print f.name
with open('/...url.../'+f.name,'wb+') as des:
for chunk in f.chunks():
des.write(chunk)
return render_to_response('index.html')

完毕