gpt4 book ai didi

android - 使用 Cordova 下载文件

转载 作者:太空狗 更新时间:2023-10-29 13:30:04 25 4
gpt4 key购买 nike

尽管在文档或本论坛中找到了许多示例,但我找不到使用 Cordova 下载文件的方法...

首先,我得到 rootFS:

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);
}
);

我在控制台中收到此错误:Uncaught TypeError:

Cannot call method 'getFile' of undefined

uri在config.xml中授权。

最佳答案

我认为问题出在这一行:imageDir = rootFS.getDirectory("imagesContent", {create: true},function(), function()) 我不认为调用 getDirectory 应该实际返回目录名称,就像您期望的那样,这就是为什么您的 imageDir 对象未定义的原因。

使用 FileTransfer.download() 方法从服务器下载内容可能更容易:http://docs.phonegap.com/en/edge/cordova_file_file.md.html#FileTransfer

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=="
}
}
);

关于android - 使用 Cordova 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com