How do I open a local PDF file in a Windows 8 / Cordova app?
I have an IOS app that I have built and am now tasked with upgrading to Windows 8 for use on a Windows tablet. My app downloads files from Dropbox that are stored in a local folder. I can see that it all works great. I can link to images using ms-appdata:///local/" + filename
the src of my img tag and I can even play mp4s from the same folder using HTML5 tags.
My problem is that for my IOS version, I used the Cordova InAppBrowser program to open local PDFs, but it doesn't work on this Windows 8 version.
I am using the following code ( filename
equals [1]CaseStudy-AC_EN_04.pdf
and it exists on the filesystem):
var ref = window.open("ms-appdata:///local/" + filename, '_blank', 'location=no');
And I am getting the following error in Visual Studio when starting the Simulator
APPHOST9607: The app can't launch the URI at ms-appdata:///local/[1]CaseStudy-AC_EN_04.pdf because of this error: -2147024846.
I tried to switch to WinJS encoding techniques, even tried to load the PDF in an iFrame, but nothing worked. I don't mind pushing the user in Internet Explorer if I have to ... I just need the user to be able to see these local PDFs. Is this a permissions issue? I only have a config.xml file, not an application manifest file, so maybe I'm missing the settings?
Does anyone have any experience?
source to share
In case anyone else has this problem. I was able to do what I wanted with this WinJS code (make sure you include the WinJS framework file)
//this is just the filename, you can probably skip this step but my filenames are from downloaded files so they could be encoded.
fileName = decodeURIComponent(fileName);
//get the local folder that contains the downloaded files
var applicationData = Windows.Storage.ApplicationData.current;
var localFolder = applicationData.localFolder;
//grab the file and return a promise
localFolder.getFileAsync(fileName)
.then(function(file) {
//launch the file - this command will let the OS use it default PDF reader - win 8 app 'reader' works great.
Windows.System.Launcher.launchFileAsync(file);
});
What is it.
@Matthew Corway works great. But extra attention is needed when the file is inside a subfolder like below:
var fullPath = "\ folderName \ fileName.pdf"
source to share