菜鸟教程小白 发表于 2022-12-12 09:40:01

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(&#34;http://dl.dropbox.com/u/97184921/editme.txt&#34;);

      fileDownloadEdit.download(
                uri,
                pathToRoot + &#39;/editme.txt&#39;,
                function(entry) {
                  console.log(&#34;download complete: &#34; + entry.fullPath);
                  alert(&#34;File Downloaded. Click &#39;Read Editable Downloaded File&#39; to see text&#34;);
                },
                function(error) {
                  console.log(&#34;download error source &#34; + error.source);
                  console.log(&#34;download error target &#34; + error.target);
                  console.log(&#34;upload error code&#34; + 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;
&#34;http_status&#34; = 404;
source = &#34;http://dl.dropbox.com/u/97184921/Internship%2520Stuff/Holidays.pdf&#34;;
target = &#34;/Users/administrator/Library/Application Support/iPhone Simulator/6.1/Applications/AF96D141-0CE5-4D60-9FA8-8A8F9A999C81/Documents/Holidays.pdf&#34;;
}
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(&#34;http://dl.dropbox.com/u/97184921/Internship%20Stuff/Holidays.pdf&#34;);

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

<p>我用来调用函数的 HTML 是;</p>

<pre><code>&lt;button onclick=&#34;downloadPDFFile();&#34;&gt;Download PDF File&lt;/button&gt; &lt;br&gt;
</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]
查看完整版本: javascript - 使用PhoneGap 下载PDF 以供离线查看?