使用PhoneGap下载PDF以便离线查看?

时间:2013-03-14 11:09:39

标签: javascript ios cordova pdf download

我正在开发一个iPhone应用程序,用户可以在Xcode中使用PhoneGap和HTML下载文件(.pdf,.docx等),然后查看它们“在线”(基本上用户查看文件他们从)或“离线”下载的网站(用户预先下载了它们,现在正在硬编码正在下载的文件,并且可以从设备上的LocalFileSystem查看文件)。我可以下载.txt文件;

    function downloadEditFile() {
        var fileDownloadEdit = new FileTransfer();
        var uri = encodeURI("http://dl.dropbox.com/u/97184921/editme.txt");

        fileDownloadEdit.download(
                uri,
                pathToRoot + '/editme.txt',
                function(entry) {
                    console.log("download complete: " + entry.fullPath);
                    alert("File Downloaded. Click 'Read Editable Downloaded File' to see text");
                },
                function(error) {
                    console.log("download error source " + error.source);
                    console.log("download error target " + error.target);
                    console.log("upload error code" + error.code);
                }
        );
    }

但是当我尝试开发它以下载.pdf文件时,它会中断。我需要帮助,我哪里错了?我真正做的就是将文件名更改为Holidays.pdf,但console出现了说法;

2013-03-14 11:06:47.921 TestApp1[265:15b03] -[CDVFileTransfer download:] [Line 313] File Transfer downloading file...
2013-03-14 11:06:48.553 TestApp1[265:15b03] File Transfer Finished with response code 404
2013-03-14 11:06:48.553 TestApp1[265:15b03] -[CDVFileTransferDelegate connectionDidFinishLoading:] [Line 437] Write file /Users/administrator/Library/Application Support/iPhone Simulator/6.1/Applications/AF96D141-0CE5-4D60-9FA8-8A8F9A999C81/Documents/Holidays.pdf
2013-03-14 11:06:48.554 TestApp1[265:15b03] FileTransferError {
code = 3;
"http_status" = 404;
source = "http://dl.dropbox.com/u/97184921/Internship%2520Stuff/Holidays.pdf";
target = "/Users/administrator/Library/Application Support/iPhone Simulator/6.1/Applications/AF96D141-0CE5-4D60-9FA8-8A8F9A999C81/Documents/Holidays.pdf";
}
2013-03-14 11:06:48.556 TestApp1[265:15b03] [LOG] download error source http://dl.dropbox.com/u/97184921/Internship%2520Stuff/Holidays.pdf
2013-03-14 11:06:48.556 TestApp1[265:15b03] [LOG] download error target /Users/administrator/Library/Application Support/iPhone Simulator/6.1/Applications/AF96D141-0CE5-4D60-9FA8-8A8F9A999C81/Documents/Holidays.pdf
2013-03-14 11:06:48.557 TestApp1[265:15b03] [LOG] upload error code3

出了什么问题?我是Xcode和PhoneGap的新手,所以任何帮助都会很棒:)

以下是下载pdf文件的确切JavaScript;

    function downloadPDFFile() {
        var fileDownloadPDF = new FileTransfer();
        var uri = encodeURI("http://dl.dropbox.com/u/97184921/Internship%20Stuff/Holidays.pdf");

        fileDownloadPDF.download(
                                  uri,
                                  pathToRoot + '/Holidays.pdf',
                                  function(entry) {
                                  console.log("download complete: " + entry.fullPath);
                                  alert("File Downloaded. Click 'Read Editable Downloaded File' to see text");
                                  },
                                  function(error) {
                                  console.log("download error source " + error.source);
                                  console.log("download error target " + error.target);
                                  console.log("upload error code" + error.code);
                                  }
                                  );
    }

和我用来调用函数的HTML是;

<button onclick="downloadPDFFile();">Download PDF File</button> <br>

1 个答案:

答案 0 :(得分:1)

看起来您的网址有误。我认为JS实际上是在翻译URI字符串中的%。 (JS的第3行)

如果你看一下控制台输出中的URL,那就是“实习%2520 ......”而不是“实习%20”。 很确定这就是你在文件上获得404的原因,这显然意味着它无法下载。

将您的数据库文件夹重命名为没有空格...使用下划线或只使用一个单词。