Skip to content Skip to sidebar Skip to footer

How To Upload A Pdf File To Server In Android?

I referred some previously asked questions but not get proper solution.I am creating an Application and want to send PDF file by selecting it from File Manager. Thanks any type of

Solution 1:

Only you have to change this lines of code when you have to select PDF file from gallery . intent.setType("application/pdf") this will search only PDF files from gallery.

Intentintent=newIntent();
    intent.setType("application/pdf");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PDF);

Solution 2:

Use Okhttp library like this, it is the simplest way to do it. But You have to change your server(API or PHP) code according to this.

RequestBodyrequestBody=newMultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("variable", fileName1,
                    RequestBody.create(MediaType.parse(fileType1), file))
            .addFormDataPart("key", "")


            .build();
    Requestrequest=newRequest.Builder().url("server url goes here").post(requestBody).build();
    okhttp3.Callcall= client.newCall(request);
    call.enqueue(newCallback() {

        @OverridepublicvoidonFailure(Call call, IOException e) {
            System.out.println("Registration Error" + e.getMessage());
        }

        @OverridepublicvoidonResponse(Call call, okhttp3.Response response)throws IOException {

            try {
                Stringresp= response.body().string();
                Log.v("Docs", resp);

            } catch (IOException e) {
                System.out.println("Exception caught" + e.getMessage());
            }
        }

    });

Post a Comment for "How To Upload A Pdf File To Server In Android?"