Gericke
Gericke

Reputation: 2241

Unable to view pdf file using PhoneGap

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

Upvotes: 1

Views: 104

Answers (1)

suraj mahajan
suraj mahajan

Reputation: 870

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

Upvotes: 1

Related Questions