作者:herozhx | 来源:互联网 | 2023-09-13 18:40
form表单文件上传
路由
# from表单上传
path(‘formupload/‘,apply.formupload,name=‘formupload/‘),
方法
# form表单文件上传
def formupload(request):
if request.method == ‘POST‘:
image_obj = request.FILES.get(‘headimg‘)
print(image_obj)
with open(settings.MEDIA_ROOT+‘/‘+image_obj.name,‘wb‘) as f:
for lin in image_obj:
f.write(lin)
return HttpResponse(‘ok‘)
前端html
上传成功示例
#ajax上传
路由
# ajax上传文件
path(‘ajaxupload/‘,apply.ajaxupload,name=‘ajaxupload/‘),
方法
# ajax文件上传
def ajaxupload(request):
if request.method == ‘POST‘:
image_obj = request.FILES.get(‘headimg‘)
print(image_obj)
with open(settings.MEDIA_ROOT + ‘/‘ + image_obj.name, ‘wb‘) as f:
for lin in image_obj:
f.write(lin)
return HttpResponse(‘ok‘)
前端html
done。