Phonegap FileTransfer()错误未定义FileTransfer

时间:2015-07-23 09:53:56

标签: html5 cordova phonegap-plugins phonegap-build

我已经阅读了大部分关于此的问题,到目前为止没有任何帮助,这就是我在这里问的原因。

点击此处时,我试图下载.pdf:

public static function findByUsername($username)
    {
        $con = array('$or' => array(array('username' => $username),array('email' => $username)));
        return static::findOne($con);
    }

这是函数(我在标题中的标记之间使用它):

   <a onclick="BeginDownload()" class="link" href="#">My guides</a>

我添加了文件和文件传输插件,这是证据: enter image description here 这就是我的config.xml的样子:

document.addEventListener("deviceready", init, false);
function init()
{
BeginDownload();
}

    function BeginDownload() {
    var fileDownloadPDF = new FileTransfer();
    var uri = encodeURI("http://www.ltz.de/de-wAssets/docs/management-guides/en/ltz-management-guide-brown-lite-en2013.pdf");

    fileDownloadPDF.download(
                              uri,
                              pathToRoot + '/andrei.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);
                              }
                              );
}

1 个答案:

答案 0 :(得分:0)

你的&#34; pathToRoot&#34;被设定?你的&#34;开始下载&#34;事件是否被触发?

尝试这样的事情:

var fileTransfer = new FileTransfer();
var uri = encodeURI("http://www.ltz.de/de-wAssets/docs/management-guides/en/ltz-management-guide-brown-lite-en2013.pdf");
var downloadProgress = 0;
var filename = url.split("/");

window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory, function(oEntry){
    var pathToRoot = oEntry.toURL() + "/path/to/file/" + filename[filename.length - 1];

    fileTransfer.onprogress = function(result){
        downloadProgress =  result.loaded / result.total * 100;
        downloadProgress = Math.round(downloadProgress);
        console.log("progress: " + downloadProgress);
    };

    fileTransfer.download(
            uri,
            pathToRoot,

            function (entry) {
                console.log("download complete: " + entry.toURL());
            },

            function (error) {
                console.log("download error source " + error.source);
                console.log("download error target " + error.target);
                console.log("upload error code: " + error.code);
    });

}, function(){ console.log("error");});