我想将图片从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(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引发此MultiValueDictKeyError异常。有什么问题?感谢