django中文件上传

前端

<form action="" method="post" enctype="multipart/form-data">
    <p><input type="file" name="myfile"></p>
    <input type="submit">

后端

def upload(request):
    if request.method == 'POST':
        file_obj = request.FILES.get('myfile')
        file_name = file_obj.name
        with open(file_name, 'wb') as f:
            # for line in file_obj:
            for line in file_obj:
                f.write(line)

    return render(request, 'upload.html')
posted @ 2019-04-16 16:22  不会玩python  阅读(6)  评论(0编辑  收藏  举报