1

I am working an app in the phonegap android in which i want to upload a file (image and pdf) to the server. What i want is when an user will click on the upload button an option dialog will open from where the user will select a file to be uploaded and then when user selects the image/pdf file the file will be uploaded. Image upload is working fine using below code:

var options = {
            destinationType: Camera.DestinationType.FILE_URI,
            sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
            quality : 50
        };

        $cordovaCamera.getPicture(options).then(function(file) {
            //success
        }, function(error) {
            //error
        });

Am using ngcordova camera plugin: http://ngcordova.com/docs/plugins/camera/

Can anyone tell me how to upload pdf using this plugin? If pdf upload is possible with any other plugin please share. Any help is appreciated.

Thanks in advance.

1 Answer 1

2

You can use ngcordova's $cordovaFileTransfer, which you can use by adding the relating cordova plugin:

cordova plugin add cordova-plugin-file-transfer

Example code taken from the docs:

module.controller('MyCtrl', function($scope, $cordovaFileTransfer) {

    var server = "http://yourserver.net/path/to/upload";
    var target = cordova.file.documentsDirectory + "test.pdf";
    var options = {};

    document.addEventListener('deviceready', function () {

        $cordovaFileTransfer.upload(server, target, options)
          .then(function(result) {
            // Success!
          }, function(err) {
            // Error
          }, function (progress) {
            // constant progress updates
          });

    }, false);
Sign up to request clarification or add additional context in comments.

3 Comments

Hey thank you But in above example file is hardcoded I mean "test.pdf". Is it possible to upload file from phone memory (user will select)?
@Unknown : have you got solution. Please share with me or give me plugin name.. I am also looking for same.
@KhurshidAnsari: You can use above mentioned plugin.

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.