Cordova FileTransfer插件无法在Android 5 +

时间:2017-05-30 10:21:50

标签: javascript android cordova file-transfer phonegap

我正在使用PhoneGap Build构建应用,仅针对Android。其中一项功能是将文件从Web服务器下载到设备。

此代码适用于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) {
	var download_link = encodeURI(URL);

	var directoryEntry = fileSystem.root; // to get root path of directory
	directoryEntry.getDirectory(Folder_Name, {
		create: true,
		exclusive: false
	}, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
	var rootdir = fileSystem.root;
	var fp = rootdir.toURL(); // Returns Fullpath of local directory

	fp = fp + "/" + Folder_Name + "/" + File_Name; // fullpath and name of the file which we want to give
	console.log("Path to local file: " + fp);
	// download function call
	filetransfer(download_link, fp);
}

function onDirectorySuccess(parent) {
	console.log('directory created successfully');
	// Directory created successfuly
}

function onDirectoryFail(error) {
	//Error while creating directory
	console.log("Unable to create new directory: " + error.code);
}

function fileSystemFail(evt) {
	//Unable to access file system
	console.log(evt.target.error.code);
}

function filetransfer(download_link, fp) {
	var fileTransfer = new FileTransfer();
	// File download function with URL and local path
	fileTransfer.download(download_link, fp,
		function(entry) {
			alert('The file was successfully downloaded, you can access it in /' + Folder_Name + '/' + File_Name + '.');
			console.log("download complete: " + entry.fullPath);
		},
		function(error) {
			//Download abort errors or download failed errors
			console.log("download error source " + error.source);
			console.log("download error target " + error.target);
			console.log("download error code" + error.code);
		}
	);
}

控制台中没有任何内容,没有错误,也没有任何日志行,这意味着没有任何回调被触发。

有没有其他人有这个神秘的问题?

由于

1 个答案:

答案 0 :(得分:0)

尝试安装这些版本。

phonegap plugin remove cordova-plugin-file
phonegap plugin remove cordova-plugin-file-transfer

phonegap plugin add cordova-plugin-file@4.3.3
phonegap plugin add cordova-plugin-file-transfer@1.5.1

我使用这些版本,我在Anrdoid 5,6,7中没有问题。试试吧。