django 获取 request.FILES.get(‘userList’) 为空
- 需要声明form的数据各式 enctype=“multipart/form-data”
- enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码。
<form action="{% url 'addUserList' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="userList" accept="application/vnd.ms-excel">
<input type="submit" value="确认导入">
</form>
class AddUserFromExcel(View):
def get(self, request):
return render(request, 'addUserList.html')
def post(self, request):
# userList 是 html 中指定的上传文件名 name="userList"
f = request.FILES.get('userList')
with open('user.xls', 'wb') as dest:
for chunk in f.chunks():
dest.write(chunk)
return HttpResponse("上传成功")