javascript - 使用PhoneGap 下载PDF 以供离线查看?
<p><p>我正在开发一个 iPhone 应用程序,用户可以在其中使用 Xcode 中的 PhoneGap 和 HTML 下载文件(.pdf、.docx 等),然后“在线”查看它们(<em>基本上用户从下载它们的网站</em>)或“离线”(<em>用户预先下载它们的位置,现在正在硬编码正在下载的文件,并且可以从设备上的 LocalFileSystem 查看文件</em>)。我可以很好地下载一个.txt文件;</p>
<pre><code> 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);
}
);
}
</code></pre>
<p>但是当我尝试开发它以下载 .pdf 文件时,它会中断。我需要帮助,我哪里出错了?我真正要做的就是将文件名更改为 <code>Holidays.pdf</code>,但 <code>console</code> 会说;</p>
<pre><code>2013-03-14 11:06:47.921 TestApp1 - File Transfer downloading file...
2013-03-14 11:06:48.553 TestApp1 File Transfer Finished with response code 404
2013-03-14 11:06:48.553 TestApp1 - 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 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 download error source http://dl.dropbox.com/u/97184921/Internship%2520Stuff/Holidays.pdf
2013-03-14 11:06:48.556 TestApp1 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 upload error code3
</code></pre>
<p>怎么了?我对 Xcode 和 PhoneGap 还很陌生,所以任何帮助都会很棒:) </p>
<p>这是下载 pdf 文件的确切 JavaScript;</p>
<pre><code> 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);
}
);
}
</code></pre>
<p>我用来调用函数的 HTML 是;</p>
<pre><code><button onclick="downloadPDFFile();">Download PDF File</button> <br>
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您的网址似乎有误。我认为 JS 从字面上翻译 % 在您的 URI 字符串中。 (JS的第3行)</p>
<p>如果您查看控制台输出中的 URL,它是“Internship%2520...”而不是“Internship%20”。
很确定这就是为什么您在文件上收到 404,这显然意味着它无法下载。</p>
<p>重命名您的 DB 文件夹,使其中没有空格...使用下划线或只使用一个单词。</p></p>
<p style="font-size: 20px;">关于javascript - 使用PhoneGap 下载PDF 以供离线查看?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/15407568/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/15407568/
</a>
</p>
页:
[1]