1.
前端部分
<div class="form-group">
<span class="h3">上传文件</span>
<form method="post" enctype="multipart/form-data" action="{% url 'upload_file' %}">{% csrf_token %}
<input type="file" name="filename">
<input type="submit" class="btn btn-success pull-right" value="上传">
</form>
</div>
2.
后端部分
def upload_file(request):
print(request.FILES)
file_obj = request.FILES.get('filename')
with open('uploads/%s' % file_obj.name, 'wb+') as destination:
for chunk in file_obj.chunks():
destination.write(chunk)
return render(request, 'base.html')