JBaczuk
JBaczuk

Reputation: 14639

Where to save a PDF on Android device using FileTransfer plugin Phonegap

I am trying to download a PDF in my phonegap application and then immediately open it, but I get a File not Found error. It seems to download. Here is my code:

    // Download
    var url = "https://example/example.pdf";
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(url);
    var fileURL = 'cdvfile://localhost/persistent/com.mycompany.myApp/';
    var fileName = 'example.pdf';

    fileTransfer.download(
        uri,
        fileURL,
        function (entry) {
            alert("download complete: " + entry.fullPath);
            // Open
            cordova.plugins.fileOpener2.open(
                fileURL + fileName,
                'application/pdf',
                {
                    error: function (errorObj) {
                        alert('Error status: ' + errorObj.status + ' - Error message: ' + errorObj.message + ' ' + errorObj.fileURL);
                    },
                    success: function () {
                        alert('file opened successfully');
                    }
                }
            );
        },
        function (error) {
            alert("download error source " + error.source);
            alert("download error target " + error.target);
            alert("upload error code" + error.code);
        }
    );
    alert('Downloading...');

I'm also wondering where is the best place to save files like this, that should be available after the app is closed?

Upvotes: 0

Views: 792

Answers (2)

Abhinav
Abhinav

Reputation: 11

Even I am new to android but what I think that there is problem in URI

To open up a Uri in android it should have a structure like file:///storage/.... (there should be 3 backslashes )

You can get more details here

The best place to save a pdf can be your sdcard or internal storage. Then the pdf will be available in your device even after the application closes.

If you wish to have the same pdf file, available in different devices while testing your application in different device/ or even in emulator you can save it in the assert folder of your android project.

hope this helps

Upvotes: 1

Rupesh
Rupesh

Reputation: 444

second argument to fileTransfer.download is the location on device where you want to download pdf file. You are setting var fileURL = 'cdvfile://localhost/persistent/com.mycompany.myApp/'; Which I believe does not exist. You can use file system plugin to first get file location on your device and then save it on device. you can use below code snapet.

    var url = "https://example/example.pdf";
        var uri = encodeURI(url);
        //var fileURL = 'cdvfile://localhost/persistent/com.mycompany.myApp/';
       // var fileName = 'example.pdf';
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

                    function gotFS(fileSystem) {
                          fileSystem.root.getDirectory("Dir_NAME", {
                                create: true,
                                exclusive: false
                          }, gotDirectory, fail);
                    }

                    function gotDirectory(entry) {
                          entry.getFile('yourFileNAme', {
                                create: true,
                                exclusive: false
                          }, gotFileEntry, fail);
                    }

                    function gotFileEntry(fileEntry) {
                          filePath = fileEntry.fullPath;
                          var fileTransfer = new FileTransfer();
                          fileTransfer.download(
                                      'uri', filePath, function(entry) {
                                            console.log("success");
//perform you launch task
                                      }, function(error) {
                                            console.log("error");
                                      }, true, {});
                    }

I hope it will help you.

Upvotes: 1

Related Questions