如何在Windows 8/Cordova应用程序中打开本地PDF?

3
我有一个IOS应用程序,现在我被要求将其移植到Windows 8上,以用于Windows平板电脑。我的应用程序从Dropbox下载文件,并存储在本地文件夹中。我可以看到所有这些都能正常工作。我能够使用标签的src属性中的ms-appdata:///local/" + 文件名引用图像,甚至可以使用HTML5视频标签从同一文件夹播放mp4文件。
我的问题是,在IOS版本中,我使用Cordova的InAppBrowser打开本地PDF,但在Windows 8版本中,它不起作用。
我正在使用以下代码(filename等于[1]CaseStudy-AC_EN_04.pdf,并且确实存在于文件系统中):
 var ref = window.open("ms-appdata:///local/" + filename, '_blank', 'location=no');

当我运行模拟器时,Visual Studio会出现以下错误。
APPHOST9607: The app can't launch the URI at ms-appdata:///local/[1]CaseStudy-AC_EN_04.pdf because of this error: -2147024846.

我已经尝试过使用WinJS编码方法,甚至尝试在iFrame中加载PDF文件,但是都没有起作用。如果必须的话,我并不介意将用户跳转到Internet Explorer。我只需要让用户能够查看这些本地PDF文件。这是权限问题吗?我只有一个config.xml文件而没有app manifest文件,所以也许我缺少某些设置?
有人对此有经验吗?

1
可能的答案在这里:从WinJS应用程序打开PDF文件 - markp3rry
1个回答

3

如果其他人也遇到了这个问题,我使用以下WinJS代码解决了它(确保包含WinJS框架文件)。

//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's default PDF reader - win 8 app 'reader' works great.
     Windows.System.Launcher.launchFileAsync(file);
});    

就是这样。

@Matthew Corway的代码没问题。 但当文件在子文件夹中时,需要额外注意,例如:

var fullPath = "\folderName\fileName.pdf"


你能指定包含哪个WinJS文件吗?我只需要这个功能,不需要WinJS的其他东西。 - dcp3450
http://try.buildwinjs.com/#get - 包括基础文件。即使您只需要一个功能,我认为您也必须包含整个文件。 - Matthew Corway
我刚刚注意到Windows构建默认包含WinJS(请参见http://stackoverflow.com/questions/28233531/why-is-winjs-included-automatically-when-targeting-windows-8-in-cordova),因此似乎没有必要显式添加它。 - Hobo
能否从 Blob 创建文件对象?我看到了 Windows.Storage.StorageFile.createStreamedFileAsync,但没有示例。 - bhantol

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接