如何使用默认应用程序打开文件

arn*_*one 1 nativescript

我正在从DropBox下载文件.

我可以通过使用看到该文件存在File.exists(filePath)并返回true.但我无法使用File.fromPath(filePath)或使用任何一个打开文件(使用默认应用程序)openUrl(filePath).

这是我的代码:

HomePage.prototype.getDownload = function() {
var filePath = fs.path.join(fs.knownFolders.currentApp().path, "MyFile");

httpModule.getFile({
    url: "https://content.dropboxapi.com/2/files/download",
    method: "POST",
    headers: { "Content-Type": "", "Dropbox-API-Arg": JSON.stringify({"path": "/MyFolder/MyFile"}), "Authorization": "Bearer *********" },      
}, filePath).then(function (response) {
    console.log("file exists: "+fs.File.exists(filePath)); // This return true
    // tried this
    fs.File.fromPath(filePath); // Does nothing
    // tried this
    utilsutils.openUrl(filePath); // Does nothing
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*aev 5

AFAIK openUrl仅适用于Web链接,不能用于打开本地文件.并且fs.File.fromPath(filePath);只创建File的实例并且不对它做任何事情

要打开文件,您需要使用每个平台可用的本机API.例如对于android(假设您的文件是PDF):

try
{
    var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(android.net.Uri.fromFile(new java.io.File(filePath)), "application/pdf");
    application.android.currentContext.startActivity(android.content.Intent.createChooser(intent, "Open PDF..."));
}
catch (e)
{
    console.log("Missing PDF application");
}
Run Code Online (Sandbox Code Playgroud)