Problem with exporting data from the API to a JavaScript file in Excel format

Instead of receiving actual data in the response, I am getting a set of characters. However, everything works fine when I click on Download file in swagger. Can someone help me diagnose the issue?

function downloadDocFile(data: Blob, ext = 'xlsx', name = 'Export'): void {
  const downloadUrl = window.URL.createObjectURL(new Blob([data]))
  const link = document.createElement('a')
  link.href = downloadUrl
  link.setAttribute('download', `${name}-${DateTime.now().toLocaleString()}.${ext}`)
  document.body.appendChild(link)
  link.click()
  link.remove()
}

const loading = useLoading()
function handleExport() {
  loading.show()
  const url = `/bill-of-material/${bomItems.value[0]?.billOfMaterialId}/available-kits/export-xls`
    
  getApiInstance('kitting')
    .post(url, { responseType: 'blob' })
    .then((data: any) => {
      if (data) {
        downloadDocFile(data)
      }
      loading.hide()
    })
}

Please refer to the following screenshots for more information: Response Image Swagger Image

I have attempted to modify the downloadDocFile and handleExport functions, but unfortunately, none of my attempts were successful.

Answer №1

function saveExcelFile(data: Blob, extension = 'xlsx', filename = 'Export'): void {
  const url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }))
  const link = document.createElement('a')
  link.href = url
  link.setAttribute('download', `${filename}-${DateTime.now().toLocaleString()}.${extension}`)
  document.body.appendChild(link)
  link.click()
  link.remove()
}

const loadingIndicator = useLoading()
function exportDataToExcel() {
  loadingIndicator.show()
  const apiEndpoint = `/bill-of-material/${bomItems.value[0]?.billOfMaterialId}/available-kits/export-xls`
    
  getApiInstance('kitting')
    .post(apiEndpoint, { responseType: 'blob' })
    .then((responseData: any) => {
      if (responseData) {
        saveExcelFile(responseData)
      }
      loadingIndicator.hide()
    })
}

Specify the correct mime type when creating the blob. Use application/vnd.ms-excel for MS Excel File (.xlsx,. xls)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: Unable to instantiate a THREE.Scene object in Three.js due to TypeError

Recently, I decided to experiment with Three.js in order to incorporate it into a project of mine. However, upon running the initial example provided in the documentation: <html> <head> <title>My first Three.js app</title& ...

What is the proper way to display the initial content of the menu?

I am currently working on a website design for an upcoming festival. The festival spans over three days, so I have created buttons to navigate and load the content for each day separately. Is there a way for me to make the content for the first day di ...

Ways to create a looping mechanism with specified number restrictions and limitations

Can anyone assist me with this problem? I am looking to create a "looping" effect similar to the image provided. What is the logic behind this repetition? Thank you in advance for your help! Here is an example output: ...

v-treeview component triggering method execution twice upon input

I'm facing an issue with my Vue component that contains a treeview. Upon selecting a node, the goal is to update an array and display the checkbox as selected. However, I'm encountering a problem where if I select elements from one folder in the ...

Utilizing data in mongoose: A beginner's guide

I have two database models: User and Conversations. The User model has the following schema: const userSchema = mongoose.Schema({ username: String, logo: String, ..... }) and the Conversation schema is as follows: const conversationSchema = mongo ...

Traverse Through Nested JSON Data and Display it in an HTML Table using Vue

Struggling to find a way to loop through my data, I have JSON data presented like this: [ { "STATUS":"CUTTING INHOUSE", "STID":"1", "CATS":[ { "CAT":"ORIGINALS ", "ARTS":[ { "ARTNO":"GY8252", ...

Using the 'type' field as a parameter in a Vue2 component object

I need help with passing an object into my component. Here is a snippet of the object's properties: { title: "myTitle", type: "myType" } However, when I define the prop in my component like this, I get a Vue runtime warning st ...

Having trouble setting breakpoints in Chrome DevTools for my Vue application

Recently, I've encountered an issue where I am unable to place breakpoints in my Vue components as all the line numbers are greyed out. This problem started after updating Chrome to version 102.0.5005.63 from 86.0.4240.75. Could this be related to usi ...

Managing 404 Errors in Vue.js Single Page Applications with an Nginx Server

I have configured my vue-cli version 3 SPA to display a custom 404 view for any requests not found in my routes.js file, following the instructions in the official documentation: Added at the end of my routes.js file: { // catches 404 errors path: &apo ...

Changing the class when a checkbox is selected using JQuery

I’m working on a bootstrap switcher and I want to change the panel color from grey to green when the checkbox (switch) is checked. I had it working before, but after updating the switcher, it no longer functions properly. Here is the main code for the s ...

Exploring the differences between using Node.js JSON.parse during object creation versus when utilizing a getter property

This topic primarily focuses on seeking feedback on whether the current approach is optimal or if there are better ways to handle it. If you have any advice or comments on the content below, even if not directly asked for, please feel free to share. In my ...

remain on the multi drop down page for a specified duration

I have created a dropdown menu with a second level drop-down page that is a form. Now, I want the second level drop-down page to stay open longer, so I have used the following JavaScript code: <script> var timer; $(".parent").on("mouseover", functio ...

The problem with 500 localStorage not being defined in Nuxt.js is causing

Issue with [nuxt] [request error]: [unhandled] [500] Error - localStorage is not defined ۱۴:۴۲:۵۶ at isAuthenticated (C:\Users\Lenovo\Desktop\CR ...

How can componentsProps be utilized within Material-UI components?

While going through the API documentation of components like AutoComplete, StepLabel, and BackDrop, I came across the componentsProps property. However, I haven't found a clear explanation or example of how to use this prop effectively. If anyone cou ...

Challenges of performance in EmberJS and Rails 4 API

My EmberJS application is connected to a Rails 4 REST API. While the application is generally working fine, it has started to slow down due to the nature of the queries being made. Currently, the API response looks like this: "projects": [{ "id": 1, ...

Issue with Ckeditor inside a bootstrap modal

I am encountering an issue while trying to integrate ckeditor into a bootstrap modal. Whenever I attempt to use it, the functionality does not work as expected. Clicking on any icons triggers an error in the console stating Uncaught TypeError: Cannot rea ...

Font family 'anticon' is not recognized

While following a coding tutorial on YouTube, I encountered an error message that has me stumped. Despite having the correct import statement and dependency installed, the issue persists. Error message in iOS simulator: https://i.stack.imgur.com/LOVCQl. ...

Maintain the current states when returning to a previous point in time

In my Angular app, I have multiple pages that allow users to make changes such as opening tabs and pills, modals, etc. For instance, if a user opens a modal and then clicks a link within that modal that takes them to another page, I want the original page ...

When using Vue with CSS3, placing an absolute positioned element within a relative wrapper can cause issues with maintaining the

Just starting out with Vue and diving into the world of CSS3! I'm currently working on a component that you can check out here: https://codesandbox.io/s/yjp674ppxj In essence, I have a ul element with relative positioning, followed by a series of di ...

Running two different wdio.config.js files consecutively

Is it possible to run two wdio.config.js files with different configurations, one after another? Here is how the first configuration file is defined in the code: const { join } = require('path'); require('@babel/register') exports.co ...