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