Force download GET request using axios

后端 未结 6 1703
生来不讨喜
生来不讨喜 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 17:18

    this code works for me :

    let xhr = new XMLHttpRequest()
    xhr.open('POST', Vue.config.baseUrl + `order-results/${id}/export-pdf`, true)
    xhr.setRequestHeader("Authorization", 'Bearer ' + this.token())
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
    xhr.responseType = 'arraybuffer'
    xhr.send()
    xhr.onload = function(e) {
    if (this.status === 200) {
        let blob = new Blob([this.response], { type:"application/pdf" })
        let link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = 'Results.pdf'
        link.click()
    }
    

    }

提交回复
热议问题