django 接收多个文件

def get_form(request):
    # 单个文件
    ret_file = request.FILES.get("fileUpload")
    # 多个文件
    ret_files = request.FILES.getlist("fileUploads")


    # 保存单个文件
    with open('%s' % ret_file.name, 'wb') as f:
        for i in ret_file.chunks():
            f.write(i)
    
    # 保存多个文件
    for file in ret_files:
        with open('%s' % file.name, 'wb') as f:
            for i in file.chunks():
                f.write(i)

    # 默认保存在manage.py同级, 可以使用os.path.join拼接路径
        for file in ret_files:
            with open('%s' % os.path.join('tmp/', file.name), 'wb') as f:
                for i in file.chunks():
                    f.write(i)

 

posted @ 2020-04-20 19:34  船长博客  阅读(1045)  评论(0编辑  收藏  举报
永远相信美好的事情即将发生!