question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

response excel file in axios

See original GitHub issue

I have a api that return an excel file from backend, this is my code

` ‘downloadErrorDetailBlast’: function () {

  let url = '/api/' + auth2.currentUser.get().getBasicProfile().getEmail() + '/blasts/' + this.blastEmailId + '/files'      

  let options = {
    headers: {
      'Authorization': 'Bearer ' + this.$session.get('user').idToken,
      'responseType': 'application/vnd.ms-excel'
    }
  }

  axios.get(url, options)
    .then(this.downloadErrorDetailBlastSuccess)
    .catch(this.downloadErrorDetailBlastFailure)
},
'downloadErrorDetailBlastSuccess': function (response) {

    const url = window.URL.createObjectURL(new Blob([response.data], {type:'application/vnd.ms-excel'}));
    const link = document.createElement('a');
    
    link.href = url;
    link.setAttribute('download', 'Detail_Blast.xls');
    document.body.appendChild(link);
    link.click();
    console.log(response.data)
    alert("File Berhasil Didownload!")

},`

But, when I console.log my response.data, the data seems weird like this

image

I wanna download my respose.data as an excel file. What should I do?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

15reactions
mateuszkardascommented, Jul 13, 2018

try change options from

  let options = {
    headers: {
      'Authorization': 'Bearer ' + this.$session.get('user').idToken,
      'responseType': 'application/vnd.ms-excel'
    }
  }

to

  let options = {
    responseType: 'blob',
    headers: {
      'Authorization': 'Bearer ' + this.$session.get('user').idToken,
    }
  }
5reactions
Koolilecommented, Aug 21, 2019

你可以用fetch

fetch(url, {
  cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
  credentials: 'same-origin', // include, same-origin, *omit
  headers: {
    'user-agent': 'Mozilla/4.0 MDN Example',
    'content-type': 'application/vnd.ms-excel;charset=UTF-8',
    'access_token': 'your_token'
  },
  method: 'GET' // *GET, POST, PUT, DELETE, etc.
})
.then(res => res.blob().then(blob => {
  const filename = decodeURI(res.headers.get('Content-Disposition').split('filename=')[1])  // 获取后端headers里面的文件名
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, filename)  // 兼容ie10
  } else {
    const a = document.createElement('a')
    document.body.appendChild(a) // 兼容火狐,将a标签添加到body当中
    a.href = window.URL.createObjectURL(blob)
    a.download = filename
    a.target = '_blank' // a标签增加target属性
    a.click()
    a.remove()  // 移除a标签
    window.URL.revokeObjectURL(url)
  }
}))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Axios Excel file download using POST results in corrupted file
I have been able to download the file using Postman so I know the file served by the endpoint is fine. I just...
Read more >
axios download excel file - Code Examples & Solutions For ...
axios download excel \ file ; 1. axios({ ; 2. url: 'http://api.dev/file-download', ; 3. method: 'GET', ; 4. responseType: 'blob', // important ;...
Read more >
Download a File with Vue and Axios - Larry Kagan
Once that's set, we'll create a method in the component to send the request via Axios and tell the browser to download (not...
Read more >
React Export Data To Excel Using Axios For Api Calls - YouTube
React Export Data To Excel Using Axios For Api Calls || Custom ExcelSheet Cell Formatting With Color.Download:// required framework an ...
Read more >
How to download excel in response from api react js - Edureka
But when I try to post some data using POST request in axios response I am getting this how to handle. API response...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found