【问题标题】:Unable to open local file using cordova inappbrowser on windows 8.1 platform无法在 Windows 8.1 平台上使用 cordova inappbrowser 打开本地文件
【发布时间】:2016-01-12 19:07:18
【问题描述】:

我正在开发一个电话间隙应用程序,我们最近添加了对 Windows 8.1 平台的支持。应用程序使用 Cordova FileSystem API 下载/创建保存到设备的文件。

我已使用如下所示的 URL 成功将文件保存到设备中

ms-appdata:///local/file.png

我已在我的 PC 上进行了检查,该文件可在应用程序根文件夹下的 LocalState 文件夹中查看。但是,当我尝试使用 inAppBrowser 打开此文件时,没有任何反应;没有报告错误消息,并且没有任何 inAppBrowser 默认事件触发。

function empty() { alert('here'); } //never fires
var absoluteUrl = "ms-appdata:///local/file.png";
cordova.InAppBrowser.open(absoluteURL, "_blank", "location=no", { loadstart: empty, loadstop: empty, loaderror: empty });

我已通过在 url 上调用以下内置 javascript 验证该 url 有效

Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).done(function (file) {
                debugger; //the file object contains the correct path to the file; C:\...etc.
            });

此外,将 url 添加为 img 标签的 src 可以正常工作。

我也尝试使用 addEventListener("loadstart") 等附加 inAppBrowser 处理程序,但它们都没有触发。但是,当我尝试打开“http://www.google.com”时,事件会触发,并且 inAppBrowser 会在屏幕上弹出。

检查 dom 后,我可以看到 inAppBrowser 元素已添加,但它似乎没有设置源属性

<div class="inAppBrowserWrap">
    <x-ms-webview style="border-width: 0px; width: 100%; height: 100%;"></x-ms-webview>
</div>

我查看了其他问题,例如this one,但无济于事。我已经验证了

a) InAppBrowser 已安装

b) deviceReady 已触发

我还尝试将目标更改为“_self”(同样的问题)和“_system”(弹出窗口说您需要一个新应用程序才能打开 msappdata:// 类型的文件),但我的想法已经不多了。有没有人遇到过类似的问题?

【问题讨论】:

    标签: windows-8.1 phonegap-plugins inappbrowser cordova-5.0.0


    【解决方案1】:

    我遇到了类似的问题。我的 cordova 应用程序下载一个文件,然后用本机浏览器打开它(以便正确处理图像、PDF 文件等)。

    最后我不得不修改InAppBrowserProxy.js 类(Windows 平台的 InAppBrowser 插件的一部分)。

    这是打开文件的代码(纯 JavaScript):

    // This value comes from somewhere, I write it here as an example
    var path = 'ms-appdata:///local//myfile.jpg';
    
    // Open file in InAppBrowser
    window.open(path, '_system', 'location=no');
    

    然后,我更新了 InAppBrowserProxy.js 文件(在 platforms\windows\www\plugins\cordova-plugin-inappbrowser\src\windows 下)。我替换了这段代码:

    if (target === "_system") {
        url = new Windows.Foundation.Uri(strUrl);
        Windows.System.Launcher.launchUriAsync(url);
    }
    

    通过这个:

    if (target === "_system") {
        if (strUrl.indexOf('ms-appdata:///local//') == 0) {
            var fileName = decodeURI(strUrl.substr(String(strUrl).lastIndexOf("/") + 1));
            var localFolder = Windows.Storage.ApplicationData.current.localFolder;
    
            localFolder.getFileAsync(fileName).then(function (file) {
                Windows.System.Launcher.launchFileAsync(file);
            }, function (error) {
                console.log("Error getting file '" + fileName + "': " + error);
            });
        } else {
            url = new Windows.Foundation.Uri(strUrl);
            Windows.System.Launcher.launchUriAsync(url);
        }
    }
    

    这是一个非常临时的 hack,但它对我有用,它可以被改进、扩展,甚至标准化。

    无论如何,可能还有其他方法可以实现这一点,只是这对我有用......

    【讨论】:

    • @Jack O'Neill 回答中提到的同一篇博文说:Windows.System.Launcher.LaunchFileAsync() can be called so that the shell determines the right app to handle the file. 这对我有用
    【解决方案2】:

    经过更多搜索,似乎PhoneGap for Windows使用的底层组件x-ms-webview仅支持加载HTML内容。 Web 视图控件上的这个 Microsoft blog post 声明

    UnviewableContentIdentified – 当用户导航到 网页以外的内容。 WebView 控件只能 显示 HTML 内容。不支持独立显示 图片、下载文件、查看Office文档等本次活动 被触发,因此应用可以决定如何处理这种情况。

    这个article 建议查看Windows.Data.Pdf 命名空间,以便为阅读PDF 提供应用内支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 2017-12-26
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多