美文网首页
文件批量上传(简版)

文件批量上传(简版)

作者: 张Boy | 来源:发表于2017-10-17 23:02 被阅读5次

模板中创建表单

<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')

完成

  • 文件上传一般还需要文件类型识别,图片压缩,归类等,此处仅写出基本的多文件上传流程。

相关文章

网友评论

      本文标题:文件批量上传(简版)

      本文链接:https://www.haomeiwen.com/subject/hxwjuxtx.html