pagekit / vue-resource

The HTTP client for Vue.js
MIT License
10.09k stars 1.6k forks source link

Response Type ArrayBuffer is not working (comparison XMLHttpRequest) #669

Open bilalcorbacioglu opened 6 years ago

bilalcorbacioglu commented 6 years ago

Hi everyone,

Simply; I used two different ways to download the document.

1-XMLHttpRequest

var oReq = new XMLHttpRequest()
oReq.open('GET', 'http://localhost:15354/api/......', true)
oReq.setRequestHeader('Accept', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
oReq.setRequestHeader('AuthenticationToken', '.......')
oReq.responseType = 'arraybuffer'
oReq.onload = function () {
    var arrayBuffer = oReq.response
    console.log(arrayBuffer)
    var blob = new Blob([arrayBuffer], { type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
    console.log(blob)
    fileSaver.saveAs(blob, 'Document.xlsx')
}
oReq.send(null)

2-Vue Resource

this.$http.get('http://localhost:15354/api/......', {responseType: 'arraybuffer'}, {headers: {'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'AuthenticationToken': '.....'}}).then(response => {
  var arrayBuffer = response.data
  console.log(arrayBuffer)
  var blob = new Blob([arrayBuffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
  console.log(blob)
  fileSaver.saveAs(blob, 'Document.xlsx')
})

Console.log Result ; http://prntscr.com/hzc2rk

3541 => 1.XMLHttpRequest (The document is opening.) 1782 => 2,Vue Resource (The document is not opening.)

Where is the problem ?

hotslab commented 6 years ago

This also happened to me too with a pdf file. The vanilla method worked whilst the vue-resource failed despite the implementation being similar. Could it be that the following snippet does not properly pass the request in the node module file /node_modules/vue-resource/src/http/client/xhr.js ?

if (request.responseType && 'responseType' in xhr) {
            xhr.responseType = request.responseType;
}  
FeherMarcell commented 5 years ago

Requesting the response in ArrayBuffer works for me (VueResource 1.5.1) if responseType: 'arraybuffer' is in the same object as headers, like this:


this.$get(
   'http://link-to-my-image', 
   { responseType: 'arraybuffer', headers: this.http_headers }
)
.then(res => {
   console.log("Response body ArrayBuffer: ", res.body)
})