一直获取不到文件 麻烦大佬给看下是哪里的问题[Python] 纯文本查看 复制代码 获取图片接口:
def post(request):
if request.method == "POST":
# 获取上传文件的处理对象,通过对象获取里面文件名称和内容
# img = request.FILES.getlist("files", None)
img = request.FILES.getlist('image')
print(img)
# print(img.pathName) # 获取文件名称
# print(img.chunks) # 获取文件内容
# 创建文件
# save_path = '{}/up_image/{}'.format(settings.MEDIA_ROOT,img.name)
# with open(save_path,'wb') as f:
# for content in img.chunks():
# f.write(content)
# # 报存到数据库
# FileUpload.objects.create(name=img.name)
return HttpResponse("ok")
else:
return HttpResponse('方法错误')
上传图片接口:
def upload(pathName,pathRoute,pathType):
'''上传图片'''
# Cookie = login(getCookie=True)
url = 'http://127.0.0.1:8000/api'
file = open(pathRoute,'rb')
print(file)
files = {
'image':(pathName,file,pathType),
'bucketName':(None,'banner'),
'type':(None,'image')
}
# image中写入的参数为:图片的名称(pathName),以二进制形式打开文件(file),文件类型(pathType)
# headers = Cookie
res = requests.post(url=url,files=files).json
return res |