基于vue。
// 静态数据
const TYPE_LIMIT = {
img: 'gif/png/jpg/jpeg/bmp',
file: 'docx/doc/xls/xlsx/pdf',
all:'gif/png/jpg/jpeg/bmp/docx/doc/xls/xlsx/pdf'
}
var myDownloader = {
template: `<van-button class="btn confirm-btn" round v-on:click="download">
{{btnText}}
</van-button>`,
props: {
url: { type: String, required: true},
filename:{type: String, default: "回执文件"},
btnText: { type: String, default: "下载"}
},
data: () => {
return {
isErrorShow: false,
errorMessage: '异常错误',
diaContent:''
}
},
mounted(){
let fileType = this.url.substring(this.url.lastIndexOf(".")+1).toLowerCase()
this.diaContent = !TYPE_LIMIT.img.split('/').includes(fileType)
? '即将跳转到文件预览,<br>请长按图片下载。'
: '即将跳转到文件预览,请点击右上角“...”并点击“默认浏览器打开”进行下载。'
},
methods: {
saveFile(){
location.href = this.url; // 直接下载
// load(this.url,this.filename) // 跨域重命名待用
},
download() {
var u = navigator.userAgent
//,app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
// 这里调用下载
function is_weixin() {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
return true;
} else {
return false;
}
}
var isWeixin = is_weixin();
if (isWeixin && isIOS) {
this.$dialog.confirm({
title: '温馨提示',
message: this.diaContent,
confirmButtonText :"确定"
})
.then(()=>{
this.saveFile()
})
.catch();
} else {
this.saveFile()
}
}
}
}