view.py:
def upload(request):
myFile =request.FILES.get("fafafa", None) # 获取上传的文件,如果没有文件>,则默认为None
if not myFile:
return HttpResponse("no files for upload!")
destination = open(os.path.join("G:\\upload",myFile.name),'wb+') # 打开特定的文件进行二进制的写操作
for chunk in myFile.chunks(): # 分块写入文件
destination.write(chunk)
destination.close()
return HttpResponse("upload over!")
模板:
<form action="/upload/" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="fafafa">
<br/>
<input type="submit">
</form>
<a href="/download_file/">
<button class="btn bg-danger">下载</button>
</a>