赞
踩
- downloadFile () {
- const _this = this
- axios({
- url: this.url,
- method: this.method,
- responseType: 'blob',
- params: this.method === 'get' ? this.data : undefined,
- data: this.method === 'post' ? this.data : undefined
- }).then(res => {
- const fileData = res.data
- const fileReader = new FileReader()
- fileReader.onload = function () {
- try {
- const jsonData = JSON.parse(this.result)
- if (jsonData.error) {
- throw new Error(`${jsonData.error.message}`)
- }
- } catch {
- // 解析成对象失败,说明是正常的文件流
- const content = res.data
- // 构造一个blob对象来处理数据
- const blob = new Blob([content])
-
- const link = document.createElement('a')
- // 获取headers中的filename文件名
- const temp = res.headers['content-disposition'].split(`filename*=UTF-8''`)[1]
- const fileName = decodeURIComponent(temp)
- link.style.display = 'none'
- link.href = URL.createObjectURL(blob)
- link.setAttribute('download', fileName)
- document.body.appendChild(link)
- link.click()
- document.body.removeChild(link)
- }
- }
- fileReader.readAsText(fileData)
- })
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。