解决跨域访问资源,a标签download属性失效的问题

it2025-11-27  6

a标签中download属性可以更改下载文件的文件名。但是如果是跨域的话,download属性就会失效。 解决方案:

<a click="downloadFile(fileUrl,fileName)">下载文件</a> function 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_29483485/article/details/103027687

最新回复(0)