Force download GET request using axios

后端 未结 6 1696
生来不讨喜
生来不讨喜 2020-12-07 16:39

I\'m using vuejs 2 + axios. I need to send a get request, pass some params to server, and get a PDF as a response. Server uses Laravel.

So

axios.get(         


        
6条回答
  •  佛祖请我去吃肉
    2020-12-07 16:57

    You're getting empty PDF 'cause no data is passed to the server. You can try passing data using data object like this

      axios
        .post(`order-results/${id}/export-pdf`, {
          data: {
            firstName: 'Fred'
          },
          responseType: 'arraybuffer'
        })
        .then(response => {
          console.log(response)
    
          let blob = new Blob([response.data], { type: 'application/pdf' }),
            url = window.URL.createObjectURL(blob)
    
          window.open(url) // Mostly the same, I was just experimenting with different approaches, tried link.click, iframe and other solutions
        })

    By the way I gotta thank you so much for showing me the hint in order to download pdf from response. Thank ya :)

                    var dates = {
                        fromDate: 20/5/2017,
                        toDate: 25/5/2017
                    }
    

    The way in which I have used is,

    axios({
      method: 'post',
      url: '/reports/interval-dates',
      responseType: 'arraybuffer',
      data: dates
    }).then(function(response) {
      let blob = new Blob([response.data], { type: 'application/pdf' })
      let link = document.createElement('a')
      link.href = window.URL.createObjectURL(blob)
      link.download = 'Report.pdf'
      link.click()
    })

提交回复
热议问题