排名
1
文章
860
粉丝
112
评论
163
.net core自定义项目模板,创建自己的模板项目,使用命令行创建模板项目
尘叶心繁 : 可以可以讲真的我都想弄个模板
net core webapi post传递参数
庸人 :
确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 : 已精
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 :
疯狂反射
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术
原
window.open 中传递Authorization header token。vue根据链接下载文件的时候传递header token

直接使用window.open去给一个链接下载文件是传递不了header token的
window.open(baseURL + 'DownloadFile/ArchivesDownloadFile?fileId=' +row.id
)
如果后端不验证token是可以这样做,但是如果要验证token就要换一种方法了。
封装根据链接下载文件的方法
function windowOpen(url, fileName) {
var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.responseType = 'blob'
// alert(55)
xhr.setRequestHeader('Authorization', 'Bearer ' + token.value)
xhr.onload = function (res) {
if (this.status === 200) {
var type = xhr.getResponseHeader('Content-Type')
var blob = new Blob([this.response], { type: type })
if (typeof window.navigator.msSaveBlob !== 'undefined') {
/*
* For IE
* >=IE10
*/
window.navigator.msSaveBlob(blob, fileName)
} else {
/*
* For Non-IE (chrome, firefox)
*/
var URL = window.URL || window.webkitURL
var objectUrl = URL.createObjectURL(blob)
if (fileName) {
var a = document.createElement('a')
if (typeof a.download === 'undefined') {
window.location = objectUrl
} else {
a.href = objectUrl
a.download = fileName
document.body.appendChild(a)
a.click()
a.remove()
}
} else {
window.location = objectUrl
}
}
}
}
xhr.send()
}
调用封装的方法
const downLoadArchive = async (e, row) => {
console.log(row)
// alert(row.fileName)
// window.open(
// baseURL + 'DownloadFile/ArchivesDownloadFile?fileId=' + row.id
// )
windowOpen(
baseURL + 'DownloadFile/ArchivesDownloadFile?fileId=' + row.id,
row.fileName
)
}
注意这里要给文件名,window.open是可以不给文件名的,只给一个下载链接即可。
下载文件的时候如果想要一个下载完成的事件,可以传递一个方法到封装的方法里边就行了
windowOpen(
baseURL + 'FileManager/DownloadContentByTemplate?fileId=' + row.id,
row.fileName + '.xls',
() => {
alert('文件下载完成!')
}
)
封装的方法里边接收一个函数的参数,在需要的地方执行就行了
function windowOpen(url, fileName, success) {
const xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.responseType = 'blob'
// alert(55)
xhr.setRequestHeader('Authorization', 'Bearer ' + token.value)
xhr.onload = function (res) {
if (this.status === 200) {
const type = xhr.getResponseHeader('Content-Type')
const blob = new Blob([this.response], { type: type })
if (typeof window.navigator.msSaveBlob !== 'undefined') {
/*
* For IE
* >=IE10
*/
window.navigator.msSaveBlob(blob, fileName)
} else {
/*
* For Non-IE (chrome, firefox)
*/
const URL = window.URL || window.webkitURL
const objectUrl = URL.createObjectURL(blob)
if (fileName) {
const a = document.createElement('a')
if (typeof a.download === 'undefined') {
window.location = objectUrl
} else {
a.href = objectUrl
a.download = fileName
document.body.appendChild(a)
a.click()
a.remove()
}
} else {
window.location = objectUrl
}
}
success()
}
}
xhr.send()
}
这里获取token的方法是vue-admin-beautiful plus版本中封装的
参考:https://www.tnblog.net/aojiancc2/article/details/7970
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价