第一种方案代码如下
// 下载附件
downloadFun(name, url) {
let that = this;
// 本地存储文件的真实路径
let filename = "file://qianduan/" + name;
// plus.io.resolveLocalFileSystemURL(path, successCallback, errorCallback);是一个HTML5+ API的方法,它用于获取本地文件系统的文件或目录对象。这个方法在HTML5+ API中是用于解析本地文件路径并获取文件对象或目录对象的。
plus.io.resolveLocalFileSystemURL(
// 表示文件或目录的本地文件系统路径
filename,
// 表示成功获取文件或目录对象时的回调函数
function (entry) {
plus.runtime.openFile(filename);
},
// 表示获取文件或目录对象失败时的回调函数
function (error) {
uni.showLoading({
title: "正在下载……",
});
let dtask = plus.downloader.createDownload(
url,
{
filename: filename,
},
function (d, status) {
uni.hideLoading();
if (status == 200) {
that.$refs.uToast.show({
title: "下载成功",
type: "success",
});
plus.runtime.openFile(d.filename);
} else {
that.$refs.uToast.show({
title: "下载失败",
type: "error",
});
plus.downloader.clear();
}
}
);
dtask.start();
}
);
},
第二种方案代码如下
//点击文件--首先检测本地有没有文件,进行以下操作
downloadFun(name,url) {
let that = this
uni.getFileInfo({
filePath: 'file://qiandaun/' + name,
success(res) {
console.log("有文件",res)
that.openFile(name)
},
fail(err){
console.log("没有文件",err)
that.downLoadOpenFile(name,url)
}
})
},
//本地无文件--下载并打开文件
downLoadOpenFile(name, url) {
let that = this
uni.showLoading({
title: '正在下载……'
});
let dtask = plus.downloader.createDownload(url, {
filename: 'file://storage/emulated/0/qipanjing/' + name
}, function(d, status) {
uni.hideLoading();
if (status == 200) {
that.$refs.uToast.show({
title: "下载成功",
type: 'success',
})
let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
console.log('d.filename',d.filename);
console.log('fileSaveUrl', fileSaveUrl);
plus.runtime.openFile(d.filename);
} else {
that.$refs.uToast.show({
title: "下载失败",
type: 'error',
})
plus.downloader.clear();
}
});
dtask.start();
},
//本地有文件--打开文件
openFile(name){
plus.runtime.openFile('file://storage/emulated/0/qipanjing/' + name);
},