MIME(媒体类型)
- application 表明是某种二进制数据
- 二进制文件没有特定或已知的 subtype,即使用 application/octet-stream。
- 响应头
- Content-Type: application/octet-stream;charset=UTF-8
- Content-Disposition: attachment;filename=文件名.xlsx
取文件名
try {
let params = response.headers['content-disposition'].split('=')[1]
let res = {
data: data,
params: params
}
return Promise.resolve(res)
} catch (err) {}
get
apiUrl = axios.get(url,params)
this.$request
.apiUrl({ params: params, responseType: 'blob'或者'arraybuffer' })
.then(res => {
let { data, params } = res
console.log('title:', params)
const blobUrl = window.URL.createObjectURL(data)
const a = document.createElement('a')
a.style.display = 'none'
a.download = params
a.href = blobUrl
a.click()
document.body.removeChild(a)
})
post
apiUrl = axios.post(url,params, {
responseType: 'arraybuffer' // 或者'blob'
})
this.$request
.apiUrl(params)
.then(res => {
let { data, params } = res
console.log('title:', params, res)
const blobUrl = window.URL.createObjectURL(data)
const a = document.createElement('a')
a.style.display = 'none'
a.download = params
a.href = blobUrl
// a.srcObject = data
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(blobUrl)
})
网友评论