Kiran
Kiran

Reputation: 20293

How to upload pdf file in hybrid mobile app using cordova and angularjs?

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.

Upvotes: 1

Views: 1919

Answers (1)

Beat
Beat

Reputation: 4412

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

Upvotes: 2

Related Questions