Last active
May 30, 2018 04:47
-
-
Save shau-lok/0abd25e4bd6cff911cd8bdf3fe992bdb to your computer and use it in GitHub Desktop.
axios 获取文件流并下载
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let path = "http://localhost:5000/api/export"; | |
axios | |
.get(path, { params: this.searchForm , responseType: 'blob'}) | |
.then(response => { | |
let data = response.data; | |
let fileName = res.headers['content-disposition'].match(/fushun(\S*)xls/)[0]; | |
var blob = new Blob([data], {type: response.headers['Content-Type']}); | |
const url = window.URL.createObjectURL(blob); | |
const link = document.createElement("a"); | |
link.href = url; | |
link.setAttribute("download", "testing.xlsx"); | |
document.body.appendChild(link); | |
link.click(); | |
}) | |
.catch(e => { | |
this.$Message.info(e); | |
}); | |
axios({ | |
url: 'http://localhost:5000/static/example.pdf', | |
method: 'GET', | |
responseType: 'blob', // important | |
}).then((response) => { | |
const url = window.URL.createObjectURL(new Blob([response.data])); | |
const link = document.createElement('a'); | |
link.href = url; | |
link.setAttribute('download', 'file.pdf'); | |
document.body.appendChild(link); | |
link.click(); | |
}); | |
responseType: 'blob', // 是必须填的 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment