axios를 사용하여 엑셀 파일 다운로드

miin·2023년 9월 4일
0

Skill Collection [Function]

목록 보기
37/46
post-thumbnail
  • 여기서 중요한점은 백으로 보낼때 responseType: 'blob'이어야 한다
    해당 타입은 params || data에 들어가지 않고 독단적으로 보냄
  const downloadCSV = async () => {
    const config = {
      method: 'post',
      responseType: 'blob',
      url:'/excel-download'
      data: {
        ...form,
      },
    }

    await AxiosApi(config).then((e) => {
      const blob = new Blob([e.data], {
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      })

      const link = URL.createObjectURL(blob)

      const excelName = document.createElement('a')
      excelName.download = `입금완료내역_${today}.xlsx`
      excelName.href = link

      document.body.appendChild(excelName)

      excelName.click()

      document.body.removeChild(excelName)
    })
  }

0개의 댓글