文件上传

def index(request):

    if request.method == 'POST':
        print(request.FILES)  # 获取文件数据
        file_obj = request.FILES.get('file')  # 文件对象
        print(file_obj)
        with open(file_obj.name, 'wb') as f:
            for line in file_obj:  # 一行一行读取内容
                f.write(line)
    return render(request, 'index.html')

 上传文件注意事项

  1、必须是post请求

  2、enctype='multipart/form-data'

 

posted @ 2022-03-01 17:02  那就凑个整吧  阅读(20)  评论(0编辑  收藏  举报