图片下载
download:function(){
wx.getImageInfo({
src:this.data.url, //这里放你要下载图片的数组(多张) 或 字符串(一张) 下面代码不用改动
success: function (ret) {
var path = ret.path;
wx.saveImageToPhotosAlbum({
filePath: path,
success(result) {
console.log("成功");
wx.hideLoading();
wx.showToast({
title: '下载图片成功',
duration: 2000,
mask: true,
});
},
fail(result) {
console.log("失败,你取消了" + JSON.stringify(result))
console.log(path);
wx.openSetting({
success: (res) => {
console.log(res);
}
})
}
});
}
});
},
文档下载
downloadFile: function(e) {
// console.log(e);
let type =this.getFileType(this.data.url)
console.log(type)
let url = this.data.url;
switch (type) {
case "pdf":
url += 'pdf';
break;
case "word":
url += 'docx';
break;
case "excel":
url += 'xlsx';
break;
default:
url += 'pptx';
break;
}
wx.downloadFile({
url: this.data.url,
header: {},
success: function(res) {
var filePath = res.tempFilePath;
console.log(filePath);
wx.openDocument({
filePath: filePath,
success: function(res) {
console.log('打开文档成功')
},
fail: function(res) {
console.log(res);
},
complete: function(res) {
console.log(res);
}
})
},
fail: function(res) {
console.log('文件下载失败');
},
complete: function(res) {},
})
},
判断文件格式
// 判断文件格式
getFileType: function (filePath) {
var startIndex = filePath.lastIndexOf(".");
if (startIndex != -1)
return filePath.substring(startIndex + 1, filePath.length).toLowerCase();
else return "";
},
<a bindtap="downloadFile" wx:if="{{isGetFile}}">文档预览</a>
<a wx:else bindtap="download">原稿下载</a>