方法一:

uni.downloadFile({
	url: obj.url,//文件地址
	success(res) {
		if (res.statusCode === 200) {
			uni.showToast({
				title: '下载成功!'
			})
			let tempFilePath = res.tempFilePath//保存下载的临时文件
			//保存临时文件到手机
			uni.saveFile({
				tempFilePath: tempFilePath,
				success(res) {
					var savedFilePath = res.savedFilePath;
				}
			})
		}
	},
	fail(err) {
		console.log(err)
	}
})

https://uniapp.dcloud.io/api/request/network-file.html#downloadfileicon-default.png?t=M5H6https://uniapp.dcloud.io/api/request/network-file.html#downloadfile

位置:/storage/emulated/0/Android/data/包名/apps/APPID7/doc/uniapp_save/文件名 

问题:不能从相册打开,位置不容易找

 

方法二

uni.showLoading({
	title:'下载中'
})
let dtask=plus.downloader.createDownload(obj.url,{
    //file://storage/emulated/0/为根目录
	filename:"file://storage/emulated/0/DCIM/"+文件名称+'.文件后缀'
},
function(d,status){
	if(status==200){
		uni.hideLoading()
		uni.showModal({
			content:'下载成功,是否打开该文件',
			complete(res) {
				if(res.confirm){
					plus.runtime.openFile(d.filename)
				}
			}
		})
		let fileSaveUrl=plus.io.convertLocalFileSystemURL(d.filename)
	}else{
		uni.hideLoading()
		uni.showToast({
			icon:'error',
			title:'下载失败'
		})
		plus.downloader.clear()
	}
})
dtask.start()

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐