javascript - Cordova FileTransfer 插件无法在 Android 5+ 上运行

标签 javascript android cordova file-transfer phonegap

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

此代码在 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);
		}
	);
}

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

还有其他人遇到过这个神秘的问题吗?

谢谢

最佳答案

尝试安装这些版本。

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 中没有出现问题。尝试一下。

关于javascript - Cordova FileTransfer 插件无法在 Android 5+ 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44259585/

相关文章:

javascript - 自定义按钮数量tinymce

javascript - 背景图像未出现,尽管出现在浏览器控制台中

javascript - Google map - 覆盖多边形的标记

android - 如何验证多个复选框

javascript - 如何使用已创建的元素删除已创建的元素

java - 我如何在 Android 上访问相机胶卷/照片?

android - 从桌面到移动应用程序的 WebRTC 连接

android - 使用命令行参数启动phonegap/cordova应用程序?

ios - cordova phonegap 状态栏不工作

jQuery 跨域 Ajax