6
3

More than 3 years have passed since last update.

【Nuxt Vue】ファイルダウンロード(エクセル)する。

Last updated at Posted at 2020-06-10

サーバー側の処理は問題ないことを確認したが、クラアント側でうまく受け取れずに少しハマってしまいました。
また似たようなことがありそうなのでメモ。

サーバー側の処理

expressを使用、詳細は省略。

Server.js
app.get('/download_excel_file', async(req, res) => {
  try {
    const excelPath = 'エクセルファイルのパス/ファイル名.xlsx'
    return res.status(200).download(excelPath)
  } catch (err) {
    return res.status(500)
  }
}

クライアント側の処理

こちらも詳細は割愛しますが、要は { responseType : 'blob' } が必要で、抜けてしまっていた為にうまく受け取れませんでした。
responseTypeが何かわからない、という方はこちら参照願います。
https://developer.mozilla.org/ja/docs/XMLHttpRequest/Sending_and_Receiving_Binary_Data

client.js
getExcelFile() { 
  try {
    this.$axios.$get('/download_excel_file', { responseType : 'blob' }).then(response => {
       let link = document.createElement('a')
       link.href = window.URL.createObjectURL(response)
       link.download = 'ファイル名.xlsx'
       link.click()
    }
  } catch(error) {
    console.log('エラー!!')
  }
}

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3