方法一:
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)
}
})
位置:/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()