参考链接:https://www.html5plus.org/doc/zh_cn/downloader.html#plus.downloader.createDownload
https://my.oschina.net/u/4489154/blog/5399978
/**
* 文书下载
*/
downloadBtn(){
if(!this.rowData.pdfId){
this.$ljToast('该文书无法下载')
return false;
}
let nowTime = getNowTimeNumber();
let fileId = this.rowData.pdfId;
let fileName = 'file://storage/emulated/0/重庆市城市管理执法指挥调度智能平台下载/行政强制(暂扣)'+ + nowTime + '.pdf'
let fileUrl = `${window.webConfig.defultJavaFilePath}/file/downLoad/App/${fileId}`;
let that = this
let loading = window.plus.nativeUI.showWaiting("正在下载文件...");
let p = -1;
let download = window.plus.downloader.createDownload(fileUrl, {filename:fileName,method:'GET'}, function (d, status) {
window.plus.nativeUI.closeWaiting();
if (status === 200) {
console.log('文件',d);
// window.plus.io.convertLocalFileSystemURL(d.filename);
window.plus.nativeUI.alert("下载完成!");
// window.plus.runtime.openFile()
} else {
window.plus.nativeUI.alert("下载失败!");
}
});
download.addEventListener("statechanged", (task) => {
if (task.state === 3) {
let x = (task.downloadedSize / task.totalSize * 100).toFixed(0)
if (x !== p) {
p = x
loading.setTitle("下载文件" + x + '%')
}
}
}, false);
download.start();
},
- uniapp嵌套 H5点击下载
goDownload(){
console.log('点击下载',JSON.parse(this.queryObj._attachmentIds))
this.$uni.postMessage({
data:{
messAgeData:{
code:'download',
attachmentIds:JSON.parse(this.queryObj._attachmentIds)
}
}
})
},
case-mobile项目
image.png
case "download":
messAgeData.attachmentIds.forEach((v) => {
console.log('遍历1',v)
v.fileName = `${v.fileName}.${v.extension}`;
let path =
config.api + setting.http.module.oss + v.downPath.slice(1);
console.log('遍历2path',path)
let loading = plus.nativeUI.showWaiting("正在下载文件...");
let p = -1;
let downLoader = plus.downloader.createDownload(
path,
{
method: "GET",
filename:
"file://storage/emulated/0/重庆市城市管理执法指挥调度智能平台下载/" +
v.fileName,
},
function (download, status) {
plus.nativeUI.closeWaiting();
if (status === 200) {
plus.nativeUI.alert("下载完成,请到手机文件管理中查看");
} else {
uni.showToast({
title: "下载失败,请重试",
});
}
}
);
downLoader.addEventListener("statechanged", (task) => {
if (task.state === 3) {
let x = (task.downloadedSize / task.totalSize * 100).toFixed(0)
if (x !== p) {
p = x
loading.setTitle("下载文件" + x + '%')
}
}
}, false);
downLoader.start();
});
// lj.redirectToByName(messAgeData.pageName,{code:'General'})
break;
case "download备份":
return;
messAgeData.attachmentIds.forEach((v) => {
v.fileName = `${v.fileName}.${v.extension}`;
let path =
config.api + setting.http.module.oss + v.downPath.slice(1);
console.log("下载的path", path);
let downLoader = plus.downloader.createDownload(
path,
{
method: "GET",
filename: "_downloads/image/" + v.fileName + `.${v.extension}`,
// filename: 'file://storage/emulated/longjin/' + v.fileName
},
function (download, status) {
console.log(JSON.stringify(download), `========>download`);
if (status === 200) {
// let fileName = plus.io.convertLocalFileSystemURL(download.filename);
let fileName = download.filename;
/**
* 保存至本地相册
*/
plus.gallery.save(
fileName,
function () {
// uni.showToast({
// duration: 3000,
// icon:'none',
// title:''
// })
uni.showModal({
title: "保存成功",
content: `文件已保存在:Android/data/io.dcloud.HBuilder/${fileName.substr(
1
)}`,
confirmText: "确定",
showCancel: false,
success: function (res) {
if (res.confirm) {
console.log("用户点击确定");
} else if (res.cancel) {
console.log("用户点击取消");
}
},
});
},
function () {
window.open(downLoader);
return;
uni.showToast({
title: "保存失败,请重试",
icon: "error",
});
}
);
} else {
uni.showToast({
title: "下载失败,请重试",
});
}
}
);
downLoader.start();
});
// lj.redirectToByName(messAgeData.pageName,{code:'General'})
break;
image.png
网友评论