Skip to content

Instantly share code, notes, and snippets.

@shau-lok
Last active May 30, 2018 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shau-lok/0abd25e4bd6cff911cd8bdf3fe992bdb to your computer and use it in GitHub Desktop.
Save shau-lok/0abd25e4bd6cff911cd8bdf3fe992bdb to your computer and use it in GitHub Desktop.
axios 获取文件流并下载
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