3

I want to upload pdf or doc file from android device to php. I got the filePath in onActivityResult method.

Uri selectedFileUri = data.getData();
 String path = selectedFileUri.getPath();

I want to know that if there is any method by which I can send file to server? Iam sending image file to server by encoding it to base64 but how can I upload pdf or doc.

Khushal Chouhan
  • 103
  • 1
  • 7
  • It is normal file upload operation. Are you using any php framework on server side? Check this link http://stackoverflow.com/a/4126746/3843374 – Paritosh Mar 09 '15 at 09:04
  • yes i am using php on server side . I can deal with it. But I want to know that what should I send from android device – Khushal Chouhan Mar 09 '15 at 09:12
  • `Iam sending image file to server by encoding it to base64 but how can I upload pdf or doc.`. You do not have to change anything of your code. At least if you did not use Bitmap or BitmapFactory to upload that image file. A file is just a file so it works always. – greenapps Mar 09 '15 at 11:04

2 Answers2

1

You can send it as byte array btw

String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
        "yourfile");
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(url);

    InputStreamEntity reqEntity = new InputStreamEntity(
            new FileInputStream(file), -1);
    reqEntity.setContentType("binary/octet-stream");
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    //Do something with response...

} catch (Exception e) {
    // show error
}

And in your php file you can get it with $_FILES.
If you want to add the value name then you can use multipart

HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

HttpPost httppost = new HttpPost("http://***.***.***.***/upload.php");
File file = new File("/sdcard/randyka.pdf");

MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("your_file", cbFile);

httppost.setEntity(mpEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

System.out.println(response.getStatusLine());

And your php will be like

<?php $_FILES['your_file']?>
Randyka Yudhistira
  • 3,612
  • 1
  • 26
  • 41
1

We have used Retrofit Library for uploading PDF file on Server. This is the best way to upload PDF file in android programmatically.

This is the Interface declaration:

@POST("/api/uploadcontroller)

Response uploadPdf(@Body TypedFile file);

Make the call in RestAdapter of the Retrofit API:->

restAdapter.create(RestApiInterface.class) // ^ interface

.uploadPdf(new TypedFile(“application/pdf”, //MIME TYPE
                            new File(pdfFilePath)));// File
Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42
Ramkailash
  • 1,852
  • 1
  • 23
  • 19