0

我在使用 Phonegap FileTransfer API 时遇到问题。目前我正在尝试运行下面的示例代码:

        saveFile: function (filepath,location, filetype) {
            console.log("FUNCTION BEING CALLED");
            var self = this;
            //filepath is path to the internets file
            //location is where on the device the file will be stored
            var fileTransfer = new FileTransfer();
            fileTransfer.download(filepath,self.rootPath + location + '.' + filetype,
                function (entry) {
                    console.log('############# DOWNLOAD WORKS ##############');
                    console.log("download complete: " + entry.fullPath);
                },
                function (error) {
                    console.log('############# DOWNLOAD FAILS ##############');
                    console.log("download error source " + error.source);
                    console.log("download error target " + error.target);
                    console.log("upload error code" + error.code);
                }
            )
        }

目前,似乎什么都没有发生。没有错误被标记,并且回调似乎没有被触发。我得到的只是“被调用的函数”的初始 console.log。

我已经仔细检查过,可以确认我包含了 cordova js 文件 ( cordova-2.4.0.js)。

如果重要的话,我已经在 Android 模拟器、HTC One X 和三星 Galaxy S3 上进行了尝试,结果都一样。

有谁知道这可能是什么原因造成的?

编辑:我也觉得我应该提到它也都在 Angular JS 应用程序中运行。

4

1 回答 1

0

这就是我在项目中的做法

 document.addEventListener("deviceready", onDeviceReady, false);

 function onDeviceReady() {
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
  }
 function gotFS(fileSystem) {
    window.fileSystem = fileSystem;
    //create directory test_app
    fileSystem.root.getDirectory("test_app", {
        create : true,
        exclusive : false
    }, dirWallpaper, fail);

 function fail(evt) {

 }

function dirWallpaper(entry) {
    //save the directory path
    window.testDir = entry;
}
//now download file to that location
saveFile: function (filepath,location, filetype) {
        console.log("FUNCTION BEING CALLED");
        var self = this;
        //filepath is path to the internets file
        //location is where on the device the file will be stored
        var fileTransfer = new FileTransfer();
        var path = window.testDir.fullPath + "/test_pdf."+ filetype;
        fileTransfer.download(filepath,path,
            function (entry) {
                console.log('############# DOWNLOAD WORKS ##############');
                console.log("download complete: " + entry.fullPath);
            },
            function (error) {
                console.log('############# DOWNLOAD FAILS ##############');
                console.log("download error source " + error.source);
                console.log("download error target " + error.target);
                console.log("upload error code" + error.code);
            }
        )
    }

试试这样,让我知道。

于 2013-04-05T15:23:17.117 回答