a標籤中download屬性可以更改下載文件的文件名。但是如果是跨域的話,download屬性就會失效。
解決方案:
<a @click="downloadFile(fileUrl,fileName)">下載文件</a>
downloadFile(url, fileName) {
var x = new XMLHttpRequest();
x.open("GET", url, true);
x.responseType = 'blob';
x.onload=function(e) {
var url = window.URL.createObjectURL(x.response)
var a = document.createElement('a');
a.href = url
a.download = fileName;
a.click()
}
x.send();
},
參考:https://blog.csdn.net/qq_41914451/article/details/104559527
下篇介紹XMLHttpRequest()對象