从android上传图像时,django中的request.FILES为空
我想从android应用程序上传图像到djnago。这是我上传图像的android代码:从android上传图像时,django中的request.FILES为空,android,python,django,image,upload,Android,Python,Django,Image,Upload,我想从android应用程序上传图像到djnago。这是我上传图像的android代码: public void upload(String filepath) throws IOException{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(SoberRequest.UPLOAD_IMAGE_URL); File file = new File
public void upload(String filepath) throws IOException{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(SoberRequest.UPLOAD_IMAGE_URL);
File file = new File(filepath);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody cbFile = new FileBody(file, "image/jpeg");
builder.addPart("image", cbFile);
HttpEntity mpEntity = builder.build();
httppost.setEntity(mpEntity);
httppost.setHeader("enctype", "multipart/form-data");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
...
}
这是我获取和保存图像的django代码:
@csrf_exempt
def get_image(request):
try:
if request.method == "POST":
destination_path = open("Images/filan.jpg", "wb+")
print(request.FILES)
image = request.FILES['image']
destination_path.write(image)
destination_path.close()
json_data = {"status": SUCCESSFUL}
return HttpResponse(json.dumps(json_data), mimetype=json_mimeType)
else:
json_data = {"status": ERROR}
return HttpResponse(json.dumps(json_data), mimetype=json_mimeType)
打印request.FILES时,它为空:。django提出了这个多值错误验证。问题是什么?谢谢您使用哪个版本的django?1)尝试检查您的请求。发布dict并确保没有任何类似于
cbFile
或类似的变量。2) 尝试更新django。旧版本中存在一些多值错误错误。3) 尝试在android端进行调试。它是否向服务器发送正确的数据?