【问题标题】:Opening a PDF in cordova javascript在科尔多瓦 javascript 中打开 PDF
【发布时间】:2016-05-20 16:52:19
【问题描述】:

我使用文件插件生成了 PDF 发票。现在我想在应用程序中打开文件。我尝试了 inAppBrowser,但它给出了一个空页面。我尝试了 fileopener,它既没有给出成功或失败的消息。如何指定文件的路径。请帮忙!!

应用内浏览器方法

var cdr='';
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dir) {
cdr=dir;
alert("cdr "+cdr);
      dir.getFile("test.pdf", {create: true, exclusive: false}, function (fileEntry) 
      {
        fileEntry.createWriter(function (writer) {
writer.onwrite = function(evt) {
     console.log(" write success");
  };

  console.log("writing to file");
     writer.write( pdfOutput );


        },function () {


          console.log("ERROR SAVEFILE");

        });
      });
    });

window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dir) {

alert("open file");
      dir.getFile("test.pdf", {create:false}, function(fileEntry) { //EXISTS

      alert("native url "+cdr.toNativeURL());
        var url =cdr.toNativeURL()  + "test.pdf";
        alert(url);
        window.open(url, '_blank');
      }, function() { //NOT EXISTS
 alert("no file found");
      });
    });

}

文件打开方法

 var cdr='';
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory ,     function(dir) {
    cdr=dir;
    console.log(" vidhya cdr "+cdr);
          dir.getFile("test.pdf", {create: true, exclusive: false}, function (fileEntry) 
          {
            fileEntry.createWriter(function (writer) {
    writer.onwrite = function(evt) {
         console.log("vidhya write success");
         openFile(cdr);
      };

      console.log("vidhya writing to file");
         writer.write( pdfOutput );


            },function () {


              console.log("vidhya ERROR SAVEFILE");

            });
          });
        });
    function openFile(cdr) {

    var fs;
    function fsSuccess(fileSystem)
    {
        fs = fileSystem;
        console.log("vidhya "+fs);
    }

    function fsFail(event)
    {
        console.log(event.target.error.code);
    }

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsFail);


        console.log("vidhya opening file " +cdr.toNativeURL());
    cordova.plugins.fileOpener2.open(
       fs.root.toURL() +'test.pdf',
        "application/pdf", //mimetype
        {
            error: function(e) {
                alert("Error Opening the File.Unsupported document format.");
            },
            success: function() {
                // success callback handler
                alert("success");
            }
        }
    );
    }

我的文件正在保存在 /internal storage/Android/Data/app_folder/files/test.pdf 中

【问题讨论】:

  • 您确定文件正在保存吗?您是否尝试过使用文件资源管理器导航到该文件?
  • 它正在保存..我可以在应用程序之外查看它。
  • 现在您已经提供了有价值的信息。在您的情况下,文件存储在应用程序数据文件夹中,因此您无法访问应用程序外部的文件,因为应用程序数据受到保护以供外部访问。您必须像我提到的那样存储在某个外部文件夹中并尝试查看。

标签: javascript cordova cordova-plugins


【解决方案1】:

这就是我让它在我的混合移动应用程序中工作的方式:

var cdr;
sessionStorage.platform = device.platform;
var fileTransfer = new FileTransfer();

if (sessionStorage.platform.toLowerCase() == "android") {
    window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
} else {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
}

function onError(e) {
    navigator.notification.alert("Error : Downloading Failed");
};

function onFileSystemSuccess(fileSystem) {
    var entry = "";
    if (sessionStorage.platform.toLowerCase() == "android") {
        entry = fileSystem;
    } else {
        entry = fileSystem.root;
    }
    entry.getDirectory("Cordova", {
        create: true,
        exclusive: false
    }, onGetDirectorySuccess, onGetDirectoryFail);
};

function onGetDirectorySuccess(dir) {
    dir.getDirectory("My_App", {
        create: true,
        exclusive: false
    }, onGetDirectorySuccess1, onGetDirectoryFail);
};

function onGetDirectorySuccess1(dir) {
    cdr = dir;
    dir.getFile(filename, {
        create: true,
        exclusive: false
    }, gotFileEntry, errorHandler);
};

function gotFileEntry(fileEntry) {
    var documentUrl = "http://myserverurl.com/test.pdf";
    var uri = documentUrl;
    fileTransfer.download(uri, cdr.nativeURL + docFileNameToView,
        function(entry) {
            openFile();
        },
        function(error) {
            navigator.notification.alert("Error");
        },
        false
    );
};

function openFile() {
    cordova.plugins.fileOpener2.open(
        cdr.nativeURL + docFileNameToView,
        'application/pdf', {
            error: function(e) {
                navigator.notification.alert("Error Opening the File.Unsupported document format.");
            },
            success: function() {

            }
        }
    );
};

function fail(error) {

    navigator.notification.alert("Error");
};

function errorHandler(e) {

    navigator.notification.alert("Error");
};

function onGetDirectoryFail(error) {

    navigator.notification.alert("Error");
};

此代码使用cordova文件传输插件下载pdf和文件打开器插件查看pdf。此示例代码还使用设备插件来获取设备平台(iOS 或 Android)和对话框插件来显示通知。

代码在 iOS 9 和 Android 6 设备上进行了测试,运行良好。在 Android 中,文件存储在 storage/emulated/0/Cordova/My_App 文件夹中

【讨论】:

  • 谢谢您..将尝试取回..您已传输文件?这是必需的吗?
  • 我现在正在尝试使用您的代码进行文件传输..但是文件没有从 url 下载..您能帮忙吗?
  • @user2281415 您能否发布任何错误跟踪并指定有关该问题的信息?
  • 实际上我收到了下载成功的消息但找不到文件fileTransfer.download('http://cdn.wall-pix.net/albums/art-space/00030109.jpg', cdr.nativeURL+'test1.jpg', function(entry) { console.log(" Successful download..."); console.log("download complete: " + entry.toURL()); openFile(); }, function(error) { console.log(" Error4 "+error); }, false );
  • @user2281415 您是在 iOS 还是 Android 上进行测试?您可以提醒 cdr.nativeURL 查找文件路径的位置,并使用文件资源管理器导航到该路径以查看文件是否已正确下载。
【解决方案2】:

如果有人在打开存储在设备中的文件时遇到问题,即使指定了正确的目标文件路径,请确保文件正确下载而没有损坏。

由于下载不当或损坏,很多时候文件打开失败。您还可以使用chrome inspect devices option 跟踪下载过程中的任何错误。还要确保使用最新版本的文件传输插件,以避免下载错误。

【讨论】:

    猜你喜欢
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多