Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

how to download a file from API in vue

i tried to download some file generated in my BE from API which work normal on other project but when i tried to use it in metronic excel can not read the file

my module:

@Action
  [SalePlan.TEST]() {
    return new Promise((resolve, reject) => {               
        ApiService.post(`/bhtt/salePlan/test`, {}, { responseType: "blob" }).then(({ data }) => {
            resolve(data);
        })
        .catch(({ response }) => {
            reject(response);
        });
    });
  }

i used this to download the file:

this.store
          .dispatch(SalePlan.TEST)
          .then((res) => {
            saveAs(
              new Blob([res]),
              "Report.xlsx"
            );
          });
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (4)


Hi,

Thank you for reaching out to us.

Could you please specify which Metronic version you are using?

Do you have any errors in your browser's console?

Regards,
Lauris Stepanovs,
Keenthemes Support Team


im using Metronic 8 and there are no errors in console regarding fetching data from the API or downloading it using file-saver. When i replace "ApiService" with "axios" it worked like normal

@Action
  [SalePlan.TEST]() {
    return new Promise((resolve, reject) => {  
         axios.post(`/bhtt/salePlan/test`,{}, { responseType: "blob" }).then(({ data }) =>{
            resolve(data);
        })
        .catch(({ response }) => {
            reject(response);
        });      
  });
  }

After further inspections i've found that the ApiService.ts file on our project was changed when we were testing and getting used to the template, im sorry for taking your time and thank you for your reply



Hi,

Sorry for the late reply.

I am glad to hear you have already managed to fix it. Please let us know if you have more questions on this topic or anything else.

Regards,
Lauris Stepanovs,
Keenthemes Support Team


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(