Phonegap FileTransfer在android
中似乎什么也不做什么我遇到了Phonegap FileTransfer API的问题。目前,我试图运行下面的示例代码:Phonegap FileTransfer在android
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);
}
)
}
目前,什么都似乎发生。没有错误被标记,并且回调似乎没有被解雇。我所得到的是“FUNCTION BEING CALLED”的初始console.log。
我已经双重检查过,并且可以确认我包含cordova js文件(cordova-2.4.0.js
)。
如果有问题,我已经在Android模拟器,HTC One X和三星Galaxy S3上尝试了这一切,所有结果都一样。
有谁知道可能是什么原因造成的?
编辑:我也觉得我应该提到它也都运行在Angular JS应用程序中。
这就是我如何做它在我的项目
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);
}
)
}
尝试像这样,让我知道。
看来我的问题可能比我第一次想到的更基础。事实证明'deviceready'事件永远不会为我开火,'window.requestFileSystem()'也没有任何作用。经过一番挖掘,并将console.logs放到cordova.js中后,我发现javascript工作正常,但当它遇到'exec()'函数时,一切就停止了。所以这导致我相信电话本身不能正常工作? – Sam 2013-04-05 15:59:04
然后我想这是项目创建的问题,尝试下载最新的phonegap并使用他们在其网站上提供的说明为android创建项目。 – 2013-04-05 19:46:55
我解决了我的问题。如果你有兴趣,当我设置这个项目时,我复制了iOS版本的代码,这意味着我试图运行app版本的cordova.js的iOS版本,而不是android版本。我害怕PEBKAC一个明确的例子。非常感谢您的帮助 – Sam 2013-04-05 23:24:27
尝试console.log(self.rootPath + location +'。'+ filetype);并检查你得到了什么 – 2013-04-05 12:27:26
这给了我'/ test_app/test_pdf.pdf'。我也尝试过没有根,这给了我'test_pdf.pdf'。这可能是问题的原因吗?我需要指定一个特定的路径吗? – Sam 2013-04-05 12:45:15
我也尝试过使用'file:/// sdcard/theFile.pdf',但我仍然没有任何东西 – Sam 2013-04-05 13:22:25