3

I asked this question before but I don't get answer. I want to upload image from android application to django. this is my android code:

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();
 ...

}

and this is my djnago code to get image:

@csrf_exempt
def get_image(request):
    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)

but when I print request.FILES , it is empty. but if I print request.body I see a field that name is "image" like this : b'--Z7unui4m0r4igQdqkvmj7DQ5rm46GmZDm\r\nContent-Disposition: form-data; name="image"\r\n\r\n\xff\xd.....". also if I set header in my android app like this :

 httppost.addHeader("Content-type", "multipart/form-data")

I get another error in my django : "Invalid boundary in multipart: None"

what is problem ?

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
Saber Solooki
  • 1,182
  • 1
  • 15
  • 34
  • I think you should have a look to this thread : http://stackoverflow.com/questions/9758982/android-django-image-upload it's quite the same as you – Laurent Meyer Aug 02 '14 at 10:44

0 Answers0