应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

window.open 中传递Authorization header token。vue根据链接下载文件的时候传递header token

6402人阅读 2020/9/29 18:28 总访问:5182657 评论:0 收藏:0 手机
分类: 前端

直接使用window.open去给一个链接下载文件是传递不了header token的

  1. window.open(baseURL + 'DownloadFile/ArchivesDownloadFile?fileId=' +row.id
  2. )

如果后端不验证token是可以这样做,但是如果要验证token就要换一种方法了。

封装根据链接下载文件的方法

  1. function windowOpen(url, fileName) {
  2. var xhr = new XMLHttpRequest()
  3. xhr.open('GET', url, true)
  4. xhr.responseType = 'blob'
  5. // alert(55)
  6. xhr.setRequestHeader('Authorization', 'Bearer ' + token.value)
  7. xhr.onload = function (res) {
  8. if (this.status === 200) {
  9. var type = xhr.getResponseHeader('Content-Type')
  10. var blob = new Blob([this.response], { type: type })
  11. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  12. /*
  13. * For IE
  14. * >=IE10
  15. */
  16. window.navigator.msSaveBlob(blob, fileName)
  17. } else {
  18. /*
  19. * For Non-IE (chrome, firefox)
  20. */
  21. var URL = window.URL || window.webkitURL
  22. var objectUrl = URL.createObjectURL(blob)
  23. if (fileName) {
  24. var a = document.createElement('a')
  25. if (typeof a.download === 'undefined') {
  26. window.location = objectUrl
  27. } else {
  28. a.href = objectUrl
  29. a.download = fileName
  30. document.body.appendChild(a)
  31. a.click()
  32. a.remove()
  33. }
  34. } else {
  35. window.location = objectUrl
  36. }
  37. }
  38. }
  39. }
  40. xhr.send()
  41. }

调用封装的方法

  1. const downLoadArchive = async (e, row) => {
  2. console.log(row)
  3. // alert(row.fileName)
  4. // window.open(
  5. // baseURL + 'DownloadFile/ArchivesDownloadFile?fileId=' + row.id
  6. // )
  7. windowOpen(
  8. baseURL + 'DownloadFile/ArchivesDownloadFile?fileId=' + row.id,
  9. row.fileName
  10. )
  11. }

注意这里要给文件名,window.open是可以不给文件名的,只给一个下载链接即可。

下载文件的时候如果想要一个下载完成的事件,可以传递一个方法到封装的方法里边就行了

  1. windowOpen(
  2. baseURL + 'FileManager/DownloadContentByTemplate?fileId=' + row.id,
  3. row.fileName + '.xls',
  4. () => {
  5. alert('文件下载完成!')
  6. }
  7. )

封装的方法里边接收一个函数的参数,在需要的地方执行就行了

  1. function windowOpen(url, fileName, success) {
  2. const xhr = new XMLHttpRequest()
  3. xhr.open('GET', url, true)
  4. xhr.responseType = 'blob'
  5. // alert(55)
  6. xhr.setRequestHeader('Authorization', 'Bearer ' + token.value)
  7. xhr.onload = function (res) {
  8. if (this.status === 200) {
  9. const type = xhr.getResponseHeader('Content-Type')
  10. const blob = new Blob([this.response], { type: type })
  11. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  12. /*
  13. * For IE
  14. * >=IE10
  15. */
  16. window.navigator.msSaveBlob(blob, fileName)
  17. } else {
  18. /*
  19. * For Non-IE (chrome, firefox)
  20. */
  21. const URL = window.URL || window.webkitURL
  22. const objectUrl = URL.createObjectURL(blob)
  23. if (fileName) {
  24. const a = document.createElement('a')
  25. if (typeof a.download === 'undefined') {
  26. window.location = objectUrl
  27. } else {
  28. a.href = objectUrl
  29. a.download = fileName
  30. document.body.appendChild(a)
  31. a.click()
  32. a.remove()
  33. }
  34. } else {
  35. window.location = objectUrl
  36. }
  37. }
  38. success()
  39. }
  40. }
  41. xhr.send()
  42. }


这里获取token的方法是vue-admin-beautiful plus版本中封装的
参考:https://www.tnblog.net/aojiancc2/article/details/7970


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价