How do I handle errors when responseType is blob using Vuejs?

iQuestProgrammer

My Question is similar to this which doesn't have an answer. I tried to search many other places but still don't have an answer.

I'm trying to download file using Axios in VueJs as a blob:

return new Promise((resolve, reject) => {
      Axios.get(`${fileDownloadUrl}`,
        { responseType: 'blob' } // Blob doesn't handle errors
      ).then(response => {
        let byteData = response.data
        var blob = new Blob([byteData], {type: response.headers['content-type']})
        let fileName = _.split(response.headers['content-disposition'], '=')
        FileSaver.saveAs(blob, fileName[1])
        resolve(fileName[1])
      },
        error => {
          console.log(error.response.data) // returns Blob - error message from service is not handled
          reject(error.response.data)
        }
      )

I removed the { responseType: 'blob' } from the above code and tried again, I get the error message now but the file downloaded doesn't have any content, it's a blank data.

How do I download the file and handle the error response returned by the service?

iQuestProgrammer

Using vue-resource solved this issue. Although it will be retiring in future releases, I couldn't find a better way to do it as Axios was not able to handle it.

Following is the code:

main.js

import VueResource from 'vue-resource'
Vue.use(VueResource)

service.js

return new Promise((resolve, reject) => {
  VueResource.http.get(`${fileDownloadUrl}`,
    { responseType: 'blob' }
  ).then(response => {
    methods.downloadFile(response, cid)
    resolve(cid)
  }, error => {
    reject(error)
  })
})

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I handle JavaScript Fetch errors?

How should I handle the errors when I try to write in DB

How do I handle errors in a worker pool using WaitGroup?

How do I handle RapidXml errors?

How do I handle dyno restarts when using Django?

How do I reference Route and ResponseType Attributes?

How do I handle errors in passport.deserializeUser()?

How do I handle errors in mapped functions in AWS Glue?

How do I handle errors that are already handled by another error?

How Do I Handle Errors Globally in TestCafe

How do i handle errors correctly and prevent showing folder directory

How to handle errors when using multiple components

How do I handle stripe errors for "redirectToCheckout"?

how does axios handle blob vs arraybuffer as responseType?

How do I handle errors in an f-string?

How do I handle different errors when working with an API?

How do I handle Import Errors?

How do I sort a list when using List Rendering in VueJS

How do I capture the exit code / handle errors correctly when using process substitution?

How do I handle errors with promises?

How do I handle transaction errors?

How do you handle errors when calling another class initializer?

When making a batch geocoding request using the Geocodio API, how can I handle 422 errors?

In React, how do I handle errors coming back from the server?

How do I handle a NULL in a VueJS filter?

How do I handle misinputs when using the substring method?

How do I handle errors in a deferred function?

How do I fix 'undefined' errors when using TypeScript Interfaces?

How do i handle errors in Dio package