1
我正在構建PhoneGap Build的應用程序,只針對Android。其中一項功能是將文件從Web服務器下載到設備。Cordova FileTransfer插件無法在Android 5+上工作
此代碼工作完全在Android 4.x的,但在Android 5.x及以上不工作:
var URL = 'https://example.com/path/to/file.pdf?auth_token=123xxxx';
var Folder_Name = 'Download';
var File_Name = URL.split('/');
File_Name = File_Name[File_Name.length - 1].split('?');
File_Name = File_Name[0];
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
function fileSystemSuccess(fileSystem) {
\t var download_link = encodeURI(URL);
\t var directoryEntry = fileSystem.root; // to get root path of directory
\t directoryEntry.getDirectory(Folder_Name, {
\t \t create: true,
\t \t exclusive: false
\t }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
\t var rootdir = fileSystem.root;
\t var fp = rootdir.toURL(); // Returns Fullpath of local directory
\t fp = fp + "/" + Folder_Name + "/" + File_Name; // fullpath and name of the file which we want to give
\t console.log("Path to local file: " + fp);
\t // download function call
\t filetransfer(download_link, fp);
}
function onDirectorySuccess(parent) {
\t console.log('directory created successfully');
\t // Directory created successfuly
}
function onDirectoryFail(error) {
\t //Error while creating directory
\t console.log("Unable to create new directory: " + error.code);
}
function fileSystemFail(evt) {
\t //Unable to access file system
\t console.log(evt.target.error.code);
}
function filetransfer(download_link, fp) {
\t var fileTransfer = new FileTransfer();
\t // File download function with URL and local path
\t fileTransfer.download(download_link, fp,
\t \t function(entry) {
\t \t \t alert('The file was successfully downloaded, you can access it in /' + Folder_Name + '/' + File_Name + '.');
\t \t \t console.log("download complete: " + entry.fullPath);
\t \t },
\t \t function(error) {
\t \t \t //Download abort errors or download failed errors
\t \t \t console.log("download error source " + error.source);
\t \t \t console.log("download error target " + error.target);
\t \t \t console.log("download error code" + error.code);
\t \t }
\t);
}
不要在控制檯中任何東西,沒有錯誤和沒有日誌行,這意味着沒有任何回調被觸發。
其他人有沒有這個神祕的問題?
謝謝
你有最新版本的文件傳輸插件嗎?我認爲他們修復了許可問題 – Akis
我在config.xml中添加了,這意味着它需要最新版本,目前爲1.6.3。 –