我正在使用 cordova-file-transfer-plugin我可以在 Android 上完美下载并播放 MP3 文件,但在 iOS 上该文件不存在。
我将目录更改为 documentsDirectory
但当我在浏览器中调用它时它不起作用。我正在使用链接上显示的代码示例。
我也看过this question但运气不佳。
var targetPath;
if (ionic.Platform.isIOS())
{
targetPath = cordova.file.documentsDirectory + data.value.split('/').pop();
} else
{
targetPath = cordova.file.dataDirectory + data.value.split('/').pop();
}
$cordovaFileTransfer.download('http://admin.lalelaknowledge.com/FileUpload/Upload/0a5eabcc-231e-4831-b72f-c8980b78615c.mp3', targetPath, {}, true)
.then(function (result)
{
console.log(result);
}, function (err) {
console.log(err);
}, function (progress) {
$timeout(function () {
console.log((progress.loaded / progress.total) * 100);
});
});
最佳答案
这是您的解决方案。您需要 cordova cordovaFileOpener2 插件来打开此文件。希望您已经安装了它。让我知道它是否有效。
$scope.initializeDownload = function() {
var fileName = '0a5eabcc-231e-4831-b72f-c8980b78615c.mp3'
var storagePath = cordova.file.dataDirectory + fileName
window.resolveLocalFileSystemURL(storagePath, function success(fileEntry) {
fileEntry.file(function(file) {
$scope.downloadAttachment(file.localURL, options, trustHosts)
})
}, function(err) {
window.resolveLocalFileSystemURL(cordova.file.documentsDirectory,
function success(dirEntry) {
dirEntry.getFile(fileName.replace(/^.*[\\\/]/, ''), {
create: true,
exclusive: false
},
function(fileEntry) {
fileEntry.file(function(file) {
$scope.downloadAttachment(file.localURL, options, trustHosts)
});
}, function(error) {
console.log(error)
});
}, function(error) {
console.log(error)
});
});
}
$scope.downloadAttachment = function(fileURL, trustHosts, options) {
var ft = new FileTransfer();
ft.onprogress = function(progress) {
// Here you can get your download status
//var downloadStatus = (progress.loaded / parseInt(fileSize)) * 100;
//console.log(downloadStatus)
};
ft.download('http://admin.lalelaknowledge.com/FileUpload/Upload/0a5eabcc-231e-4831-b72f-c8980b78615c.mp3',
fileURL,
function(result) {
//Second argument is the file Mime type
$cordovaFileOpener2.open(result.nativeURL, 'audio/mpeg').then(function(result) {
console.log("Open success")
}, function(err) {
console.log("Open Error")
});
},
function(error) {
ft.abort();
console.log(error.exception || error.body)
},
trustHosts,
options
);
}
关于ios - 如何使用 Cordova 访问 iOS 本地文件系统中的下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39162644/