【问题标题】:Cordova FileTransfer plugin not working on Android 5+Cordova FileTransfer 插件不适用于 Android 5+
【发布时间】:2017-06-25 15:49:24
【问题描述】:

我正在使用 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);
		}
	);
}

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

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

谢谢

【问题讨论】:

  • 你有最新版本的文件传输插件吗?我认为他们解决了权限问题
  • 我在 config.xml 中添加了 ,意思是它需要最新版本,目前是 1.6.3。

标签: javascript android cordova file-transfer phonegap


【解决方案1】:

尝试安装这些版本。

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 中没有问题。试试吧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 2018-11-04
    相关资源
    最近更新 更多