我正在使用以下代码下载并保存手机存储中的文件。
var downloadUrl = encodeURI(cordova.file.dataDirectory + fileName);
var fileTransfer = new FileTransfer();
fileTransfer.download(
"http://developer.android.com/assets/images/home/ics-android.png",
downloadUrl,
function(entry) {
alert("download complete: " + entry.fullPath);
},
function(error) {
alert("download error source " + error.source);
alert("download error target " + error.target);
alert("upload error code" + error.code);
});
此代码在任何android设备上均能正常运行,但在iPhone上无法正常工作,并出现以下错误。
download error source " the url used"
download error target: " the target used "
upload error code 1
我是iOS新手,有人可以帮助我解决此问题吗?并且让我知道执行此代码之前应授予的所有权限...
答案 0 :(得分:1)
文件传输插件已弃用了一段时间(超过一年),您可能不应该花费太多时间来使文件传输插件正常工作。我建议按照博客文章中有关如何transition off the file api的说明进行操作。
因此对于您的代码(未经测试,但应该可以使用):
window.requestFileSystem(cordova.file.dataDirectory, 0, function (fs) {
console.log('file system open: ' + fs.name);
fs.root.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {
console.log('fileEntry is file? ' + fileEntry.isFile.toString());
var oReq = new XMLHttpRequest();
// Make sure you add the domain name to the Content-Security-Policy <meta> element.
oReq.open("GET", "http://developer.android.com/assets/images/home/ics-android.png", true);
// Define how you want the XHR data to come back
oReq.responseType = "blob";
oReq.onload = function (oEvent) {
var blob = oReq.response; // Note: not oReq.responseText
if (blob) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function() {
console.log("Successful file write...");
};
fileWriter.onerror = function (e) {
console.log("Failed file write: " + e.toString());
};
fileWriter.write(blob);
});
} else {
console.error('we didnt get an XHR response!');
}
};
oReq.send(null);
}, function (err) {
console.error('error getting file! ' + err);
});
}, function (err) {
console.error('error getting persistent fs! ' + err);
});
答案 1 :(得分:0)
要使其在iOS中正常运行,我必须将create()放在平台就绪检查中。
constructor(platform: Platform, filetransfer: FileTransfer) {
platform.ready().then(() => {
this.Transfer = filetransfer.create();
}
}
download(fileName){
let lLocation = this.file.cacheDirectory + fileName;
this.Transfer.download(
"http://developer.android.com/assets/images/home/ics-android.png",
lLocation).then().catch
}