Phonegap build - 将远程文件下载到iOS上的文档目录

时间:2014-06-04 09:52:32

标签: ios cordova

我正在尝试将外部文件下载到使用phonegap build(phonegap 3.3)构建的iOS应用程序中。该文件已创建并下载,但此大小仅为4Kb,并且已损坏。我尝试了另一台服务器中的另一个文件,我得到了相同的结果。

这是我的代码(基于github上的文件下载示例):

    <div class="app">
        <h1>PhoneGap</h1>
        <div id="deviceready" class="blink">

        </div>
    </div>
    <script type="text/javascript" src="phonegap.js"></script>
    <script type="text/javascript">
        document.addEventListener('deviceready', onDeviceReady, false);

        function downloadFile() {
            console.log('downloadFile');
            window.requestFileSystem(
                LocalFileSystem.PERSISTENT,
                0,
                onRequestFileSystemSuccess,
                fail
            );
        }

        function onRequestFileSystemSuccess(fileSystem) {
            console.log('onRequestFileSystemSuccess');
            fileSystem.root.getFile(
                'dummy.html',
                {create: true, exclusive: false},
                onGetFileSuccess,
                fail
            );
        }

        function onGetFileSuccess(fileEntry) {
            console.log('onGetFileSuccess!');
            var path = fileEntry.toURL().replace('dummy.html', '');
            var fileTransfer = new FileTransfer();
            fileEntry.remove();

            fileTransfer.download(
                'http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf',
                path + 'theFile3.pdf',
                function(file) {
                    console.log('download complete: ' + file.toURI());
                    showLink(file.toURI());
                },
                function(error) {
                    console.log('download error source ' + error.source);
                    console.log('download error target ' + error.target);
                    console.log('upload error code: ' + error.code);
                }
            );
        }

        function showLink(url) {
            alert(url);
            var divEl = document.getElementById('deviceready');
            var aElem = document.createElement('a');
            aElem.setAttribute('target', '_blank');
            aElem.setAttribute('href', url);
            aElem.appendChild(document.createTextNode('Ready! Click To Open.'))
            divEl.appendChild(aElem);
        }

        function fail(evt) {
            console.log(evt.target.error.code);
        }

        /* When this function is called, PhoneGap has been initialized and is ready to roll */
        function onDeviceReady() {
            console.log('device ready!');
            downloadFile();
        }
    </script>

所以我在showLink函数的警报中获取了url,但文件没有加载。用ipad文件浏览器查看它,我看到文件夹中的文件,但它已损坏,只有4kb!

这是我的config.xml文件(也许我错过了一些插件或权限?):

    <?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
    xmlns:gap = "http://phonegap.com/ns/1.0"
    id        = "com.phonegap.example"
    version   = "1.0.0">

<name>download</name>

<description>
download example
</description>

<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.file" version="1.0.1" />
<gap:plugin name="org.apache.cordova.file-transfer" version="0.4.2" />

<feature name="File">
    <param name="ios-package" value="CDVFile" />
</feature>
<feature name="FileTransfer">
    <param name="ios-package" value="CDVFileTransfer" />
</feature>

<author href="https://example.com" email="you@example.com">
Your Name
</author>

</widget>

这里有什么问题?如果有人有另一个将外部文件下载到 iOS 的示例,那也没关系。

2 个答案:

答案 0 :(得分:1)

您好,您可以使用下面的代码段下载文件。这在我的项目中有效。

            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

            function gotFS(fileSystem) {
                  fileSystem.root.getDirectory("Dir_NAME", {
                        create: true,
                        exclusive: false
                  }, gotDirectory, fail);
            }

            function gotDirectory(entry) {
                  entry.getFile('yourFileNAme', {
                        create: true,
                        exclusive: false
                  }, gotFileEntry, fail);
            }

            function gotFileEntry(fileEntry) {
                  filePath = fileEntry.fullPath;
                  var fileTransfer = new FileTransfer();
                  fileTransfer.download(
                              'uri', filePath, function(entry) {
                                    console.log("success");
                              }, function(error) {
                                    console.log("error");
                              }, true, {});
            }

答案 1 :(得分:1)

您有一个成功运行的iOS下载功能。你的DOM操作有问题:

var divEl = document.getElementById('deviceready');
var aElem = document.createElement('a');
aElem.setAttribute('target', '_blank');
aElem.setAttribute('href', url);
aElem.appendChild(document.createTextNode('Ready! Click To Open.'))
divEl.appendChild(aElem);

应该是这样的:

var divEl = document.getElementById('deviceready');
var aElem = document.createElement('a');
aElem.setAttribute('target', '_blank');
aElem.setAttribute('href', "www.yeayea.com");
aElem.setAttribute('text',"ooga");
aElem.appendChild(document.createTextNode('Ready! Click To Open.'))
$(aElem).insertAfter(divEl);

Demo.