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)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM