1.配置set
# 设置上传文件路径
MDEIA_ROOT = os.path.join(BASE_DIR, 'static/upload')
2.代码的编写
def handle_upload(request):
if request.method == 'POST':
fobj = request.FILES.get('photo')
path = os.path.join(settings.STATICFILES_DIRS[0], 'upload')
path = os.path.join(path, fobj.name)
if fobj:
print(fobj.name, fobj.size)
with open(path, 'wb') as target:
if fobj.multiple_chunks():
for data in fobj.chunks():
target.write(data)
print('大于2.5')
else:
print('小于2.5')
target.write(fobj.read())
return HttpResponse("文件上传")
return render(request, 'upload.html')