a標籤跨域下載文件,解決download失效問題

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()對象

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章