2016-01-12 41 views
3

我正在开发手机间隙应用程序,最近我们添加了对Windows 8.1平台的支持。应用程序使用Cordova FileSystem API下载/创建保存到设备的文件。无法在Windows 8.1平台上使用cordova inappbrowser打开本地文件

我一直在使用它看起来像这样

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

我检查我的电脑上的文件是应用程序的根文件夹下的LocalState文件夹中查看的URL成功保存文件到设备。但是,当我尝试使用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作为的src img标签按预期工作。

我也尝试使用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但没有其他问题无济于事。我已经验证了

一)InAppBrowser安装

B)deviceReady已经解雇

我也试图改变目标为“_self”(同样的问题)和“_SYSTEM”(弹出说你需要一个新的应用程序来打开一个msappdata类型的文件://),我的想法已经不多了。有人遇到过类似的问题吗?

回答

1

经过更多搜索后,似乎PhoneGap for Windows使用的基础组件x-ms-webview仅支持加载HTML内容。此Microsoft blog post上的Web视图控件指出

UnviewableContentIdentified - 是当用户浏览到一个比其他网页内容 解雇。 WebView控件仅能够显示HTML内容 。它不支持显示独立的 图像,下载文件,查看Office文档等。此事件 已被解雇,因此应用可以决定如何处理这种情况。

article建议看Windows.Data.Pdf命名空间用于读取PDF文件提供应用程序内的支持。

3

我有类似的问题。我的科尔多瓦应用程序下载文件,然后用本机浏览器打开它(以便正确处理图像,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); 
    } 
} 

这是一个非常特设黑客,但它的伎俩对我来说,它可以改进,扩展,甚至是标准化的。

总之,可能还有其他的方法来达到这个目的,它只是这个工作对我来说...

+1

是在@Jack奥尼尔的回答中提到的相同的博客文章,说:'Windows.System。 Launcher.LaunchFileAsync()可以被调用,以便shell确定正确的应用程序来处理文件。“这对我有效 – shadi

相关问题