这段代码 url是文件路径 fileName文件名称(不好用),支持( .doc .docx .ppt .csv .pptx .xlsx .xls .rar .zip)
aBaseDown(url, fileName) {
const a = document.createElement('a');
a.href = url;
a.target = '_blank'
a.download = fileName;
a.setAttribute('download', fileName);
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
这段代码支持 图片下载 ( .jpg .jpeg .png等) baseUrl是图片路径
fetch(baseUrl)
.then(res => res.blob())
.then(blob => {
var link = document.createElement('a');
link.style.display = 'none';
document.body.appendChild(link);
let url = window.URL.createObjectURL(blob);
link.href = url;
link.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(link);
})
这段代码支持pdf ofd
pdfDown(url, fileName) {
axios(url, {
responseType: 'blob',
}).then(res => {
const url = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
console.log(link);
link.remove();
})
},```