- 前端页面代码
文件上传
{# {% url 'version:uploadFile' %}是在Django中url.py里面的地址 #}
- Django代码
view.py代码
def upload_file(request):
if request.method == 'POST':
# test是前端页面input的name
file = request.FILES.get("test")
print(file.name)
# 文件存放位置
f = open(os.path.join(settings.BASE_DIR,'static/'+file.name),'wb')
for chunk in file.chunks():
f.write(chunk)
f.close()
return HttpResponse(file.name)
elif request.method == 'GET':
return render(request, 'uploadFile.html')
url.py
urlpatterns = [
path('uploadFile/', views.upload_file, name='uploadFile'),
]
这样简单的文件上传就完成了