如何从 UWP(通用 Windows 平台)Web 应用程序启动 PDF [英] How to Launch a PDF from a UWP (Universal Windows Platform) Web Application

查看:25
本文介绍了如何从 UWP(通用 Windows 平台)Web 应用程序启动 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将现有的 Web 应用程序(HTML5、JS、CSS 等)转换为 Windows UWP 应用程序,以便(希望)我可以通过 Windows 应用商店将其分发到 Surface Hub,以便它可以脱机运行.一切正常,除了 PDF 查看.如果我在新窗口中打开 PDF,基于 Edge 的浏览器窗口就会崩溃.如果我打开一个 IFRAME 并将 PDFJS 加载到其中,它也会崩溃.我真正想做的只是将 PDF 交给操作系统,这样用户就可以在他们安装的任何 PDF 查看器中查看它.

I've converted an existing web application (HTML5, JS, CSS, etc.) into a Windows UWP app so that (hopefully) I can distribute it via the Windows Store to Surface Hubs so it can run offline. Everything is working fine, except PDF viewing. If I open a PDF in a new window, the Edge-based browser window simply crashes. If I open an IFRAME and load PDFJS into it, that also crashes. What I'd really like to do is just hand off the PDF to the operating system so the user can view it in whatever PDF viewer they have installed.

我发现了一些看起来很有前途的特定于 Windows 的 Javascript API,但我无法让它们工作.例如:

I've found some windows-specific Javascript APIs that seem promising, but I cannot get them to work. For example:

 Windows.System.Launcher.launchUriAsync(
   new Windows.Foundation.Uri(
     "file:///"+
     Windows.ApplicationModel.Package.current.installedLocation.path
       .replace(///g,"/")+"/app/"+url)).then(function(success) {
                if (!success) {

这会生成一个 file://URL,我可以将其复制到 Edge 中并显示 PDF,所以我知道 URL 内容是正确的.但是,在应用程序中它什么也不做.

That generates a file:// URL that I can copy into Edge and it shows the PDF, so I know the URL stuff is right. However, in the application it does nothing.

如果我将 https://URL 传递到该 launchUriAsync 函数中,则可以.所以看起来这个函数不喜欢 file://URLs.

If I pass an https:// URL into that launchUriAsync function, that works. So it appears that function just doesn't like file:// URLs.

我也试过这个:

Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(url).then(
  function(file) { Windows.System.Launcher.launchFileAsync(file) })

那也没有用.再次,没有错误.它只是没有做任何事情.

That didn't work either. Again, no error. It just didn't do anything.

有什么我可以尝试的其他想法吗?

Any ideas of other things I could try?

-- 更新--

查看已接受的答案.这是我最终使用的代码.(请注意,我的所有文件都在名为app"的子文件夹中):

See the accepted answer. Here is the code I ended up using. (Note that all my files are in a subfolder called "app"):

if (location.href.match(/^ms-appx:/)) {
        url = url.replace(/?.+/, "");
        Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(("app/" + url).replace(///g,"\")).then(
            function (file) {
                var fn = performance.now()+url.replace(/^.+./, ".");
                file.copyAsync(Windows.Storage.ApplicationData.current.temporaryFolder,
                    fn).then(
                    function (file2) {
                        Windows.System.Launcher.launchFileAsync(file2)
                    })
            });
        return;
    }

原来你必须把/变成 否则它找不到文件.而且 copyAsync 拒绝覆盖,所以我只使用 performance.now 来确保我总是使用新的文件名.(在我的应用程序中,PDF 的源文件名无论如何都是自动生成的.)如果你想保留文件名,你必须添加一堆代码来检查它是否已经存在等.

Turns out you have to turn the / into or it won't find the file. And copyAsync refuses to overwrite, so I just use performance.now to ensure I always use a new file name. (In my application, the source file names of the PDFs are auto-generated anyway.) If you wanted to keep the filename, you'd have to add a bunch of code to check whether it's already there, etc.

推荐答案

LaunchFileAsync 是在这里使用的正确 API.您不能直接从安装目录启动文件,因为它是受保护的.您需要先将其复制到其他应用程序可访问的位置(例如您的 PDF 查看器).使用 StorageFile.CopyAsync 在所需位置制作副本.

LaunchFileAsync is the right API to use here. You can't launch a file directly from the install directory because it is protected. You need to copy it first to a location that is accessible for the other app (e.g. your PDF viewer). Use StorageFile.CopyAsync to make a copy in the desired location.

官方 SDK 示例:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/AssociationLaunching

Official SDK sample: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/AssociationLaunching

这篇关于如何从 UWP(通用 Windows 平台)Web 应用程序启动 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆