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.

3
  • It is normal file upload operation. Are you using any php framework on server side? Check this link stackoverflow.com/a/4126746/3843374 Commented Mar 9, 2015 at 9: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 Commented Mar 9, 2015 at 9: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. Commented Mar 9, 2015 at 11:04

2 Answers 2

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']?>
Sign up to request clarification or add additional context in comments.

10 Comments

I have full file path in android. Where can I put it that. Suppse "mnt/sdcard/documents/file.pdf"
File file = new File("mnt/sdcard/documents/file.pdf");
can i send it with namevaluepair
your file name. i.e your file name is Randyka.pdf then $_FILES['randyka.pdf']
last question. How can I get filename from path??? Please tell me, I am about to complete
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.