【发布时间】:2014-03-23 09:33:56
【问题描述】:
使用 Cordova/PhoneGap 3.3.0,我正在使用 FileTransfer 插件下载文件,然后尝试使用 InAppBrowser 插件打开它。 我可以成功下载文件,并将其放在临时目录中。由于 File 插件现在使用 URL 模式,我无法弄清楚如何将正确的 url/路径传递给 InAppBrowser 插件的 window.open 方法。我也找不到任何相关文件。我能找到的所有“下载和打开”文档都已过时并且是 URL 前架构。
相关链接:
- Cordova Release info on New Plugin Versions
- The readme for the FileTransfer plugin
- The readme for the InAppBrowser plugin
- How to open local file with InAppBrowser with recent changes to URL scheme in File plugin - 类似问题
我发现的过时示例:
- File-transfer download file issue on Cordova 3.1 - 这个用户因为想不通而降级到较早的版本
-
https://gist.github.com/devgeeks/4982983 - 此示例使用
entry.fullPath,现在已弃用toURL()
这是我的代码:
var uri = encodeURI("http://some.url/file.pdf");
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0,
function (fileSystem) {
var fileTransfer = new FileTransfer();
var filename = fileSystem.root.toURL() + uri.substr(uri.lastIndexOf("/") + 1);
fileTransfer.download(uri, filename,
function(entry) { // download success
var path = entry.toURL(); //**THIS IS WHAT I NEED**
window.open(path, "_system");
},
function(error) {} // irrelevant download error
);
},
function(error) {} // irrelevant request fileSystem error
);
我目前正在 Android 上的 Nexus 7 和 Nexus 5 上进行测试。InAppBrowser 正确打开了默认的 pdf 启动器(在我的情况下为 Adobe Reader),但随后我收到“文档路径无效”错误。
[更新:显示返回值]
我已经为文件路径尝试了以下所有组合:
var path = entry.toURL(); // "cdvfile://localhost/temporary/file.pdf"
var path = entry.fullPath; // "file.pdf"
var path = fileSystem.root.toURL() + filename; // "cdvfile://localhost/temporary/file.pdf"
var path = fileSystem.root.fullPath + filename; // "/file.pdf"
【问题讨论】:
-
如果您最近更新了插件,也许您必须处理新的 URL 方案
cdvfile://? cordova.apache.org/news/2014/02/10/plugins-release.html -
感谢您的评论。我正在使用您提供的链接中引用的这些最新插件。我读了这个,但不记得我发帖的 URL。我将其添加到相关链接部分。当我打电话给
entry.toURL()时,我得到了带有cdvfile://前缀的链接 -
愚蠢的问题:为什么不用window.open直接使用原始url而不是先下载呢?
-
这不是一个愚蠢的问题。我以前这样做过,但某些 Android 设备显示“无法打开文件”。错误。这是最奇怪的事情。使用该方法,文件下载正确,我可以通过文件资源管理器在 Adobe 中打开,但是通过下载应用程序打开时,会显示错误。我认为这是一个 MIME 类型问题,但我确认使用了正确的 MIME 类型。此外,通过应用程序打开它为用户节省了一个额外的步骤,即必须单击下载的文件才能打开它(假设它无论如何都可以正常工作)。
-
也许可以尝试使用 webintent 插件而不是 inappbrowser 来打开 pdf:github.com/Initsogar/cordova-webintent
标签: cordova inappbrowser