vue下载文件重命名
文章描述:
vue下载文件的时候对文件重命名
<template>
<div class="about">
<h1>This is an about page</h1>
<div @click="download">下载</div>
</div>
</template>
<script>
import axios from "axios";
export default{
data(){
return{
file_url:'http://file.miyil.com/1234.pdf',
name:'文件'
}
},
created() {
// this.download()
},
methods:{
download(){
axios({
url:'/api/home'
}).then(res=>{
console.log(res)
})
let link = this.file_url
var url = link + '?t=' + new Date().getTime()
fetch(url).then(response => response.blob()).then(blob => {
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
let newFileName = this.name +'.pdf';
link.setAttribute('download', newFileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}).catch(
);
}
//...
}
}
</script>
发布时间:2024/04/03
发表评论