Android 使用Cordova下载文件
尽管文档或论坛中有很多例子,但我找不到使用Cordova下载文件的方法 首先,我得到rootFS:Android 使用Cordova下载文件,android,cordova,download,filesystems,Android,Cordova,Download,Filesystems,尽管文档或论坛中有很多例子,但我找不到使用Cordova下载文件的方法 首先,我得到rootFS: function gotFS(fileSystem) { console.log("got filesystem"); // save the file system for later access console.log(fileSystem.root.fullPath); // displays "/" on desktop // di
function gotFS(fileSystem) {
console.log("got filesystem");
// save the file system for later access
console.log(fileSystem.root.fullPath);
// displays "/" on desktop
// displays "file:///mnt/sdcard" on android with SD Card
window.rootFS = fileSystem.root;
}
document.addEventListener('deviceready', function() {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, function(){
console.log("error getting LocalFileSystem");
});
}, false);
上载脚本:
// Creating the image directory
imageDir = rootFS.getDirectory("imagesContent", {create: true},
function(){
// Success
},
function(error){
console.log("ERROR getDirectory");
console.log(error);
}
);
// Creating and Downloading the image
imgFile = imageDir.getFile(filename, {create: true, exclusive: true},
function (){
var localPath = rootFS.fullPath+'/imagesContent/'+filename;
fileTransfer = new FileTransfer();
fileTransfer.download('http://example.com/images/'+filename,
localPath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function (error) {
console.log(error);
console.log('download error: ' + error.code);
console.log("download error source " + error.source);
console.log("download error target " + error.target);
}
);
},
function (error){
console.log("ERROR getFile");
console.log(error);
}
);
我在控制台中得到这个错误:uncaughttypeerror:
Cannot call method 'getFile' of undefined
uri是在config.xml中授权的。我认为问题在于这一行:
imageDir=rootFS.getDirectory(“imagesContent”,{create:true},function(),function())
我不认为调用getDirectory
实际上应该返回目录名,正如您所期望的那样,这就是您的imageDir
对象未定义的原因
使用FileTransfer.download()方法从服务器下载资料可能会更容易:
我使用它,但成功和下载已完成,但在y应用程序中找不到图像这就是我试图做的。但是我需要配置
filePath
的值。这是一个可能需要创建的目录。因此,我遵循了在中给出的另一个例子;起初我误解了。我不得不删除额外的getFile
函数。但是,我注意到新目录是在SD卡中创建的。有没有一种方法可以使用应用程序目录?Hi@Yako要回答您的问题,您可以使用Cordova提供的文件系统对象,并且通过属性FileSystem.files,您可以使用应用程序文件目录。更多信息请点击此处:
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");
fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");
fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);