1

I am trying to view a pdf that has been downloaded, but have no luck displaying it.

This is my code for downloading and displaying a file. When I try to open the file I just get an alert saying unable to view file.

jQuery

// Download edition's pdf file
function downloadFile(filename){
    var localDir = cordova.file.dataDirectory;
    // Validate if file exist before download
    window.resolveLocalFileSystemURL(localDir + filename, function(){
        ShowPDF(localDir + filename);
    }, function(){
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
        function onFileSystemSuccess(fileSystem) {
            fileSystem.root.getFile(
                "index.html", {create: true, exclusive: false}, 
                function gotFileEntry(fileEntry) {
                    //var sPath = fileEntry.fullPath.replace("index.html","");
                    var localDir = cordova.file.dataDirectory;
                    var fileTransfer = new FileTransfer();
                    fileEntry.remove();

                    fileTransfer.download(
                        path + filename,
                        localDir + filename,
                        function(theFile) {
                            ShowPDF(theFile.toURI());
                            console.log("download complete: " + theFile.toURI());
                        },
                        function(error) {
                            console.log("download error source " + error.source);
                            console.log("download error target " + error.target);
                            console.log("upload error code: " + error.code);
                            console.log(error);
                        }
                    );
            }, fail);
    }, fail);
    });
};

// Show downloaded pdf
function ShowPDF(filePath){
    window.open(filePath, '_system', 'location=yes')
};

EDIT: I just realized that this part will also download the file using the browser and then you can view the downloaded file.

window.open(path + filename, '_system', 'location=yes');

I need to validate if the file has already been downloaded before displaying the local file.

Update

I followed this answer but still unable to view the local file

Community
  • 1
  • 1
Gericke
  • 2,109
  • 9
  • 42
  • 71

1 Answers1

1

You cant Open PDF in cordova webview. Try Phonegap File Opener Plugin.

suraj mahajan
  • 832
  • 1
  • 12
  • 21
  • I am currently using the fileOpener plugin but still no success – Gericke Nov 17 '15 at 06:42
  • Please be sure that you have at least one pdf reader exist in your device – suraj mahajan Nov 17 '15 at 06:44
  • Yes I do have readers on my phone. What I think could be the issue is that the location where the file is stored do not have permission perhaps to read the file. I am investigating this issue – Gericke Nov 17 '15 at 06:48