What is the process for transforming binary code into a downloadable file format?

Upon receiving a binary response from the backend containing the filename and its corresponding download type, the following code snippet illustrates the data:

01 00 00 00 78 02 00 00 6c 02 00 00 91 16 a2 3d ....x...l....... 9d e3 a6 4d 8a 4b b4 38 77 bc b1 b7 01 00 00 00 ...M.K.8w....... 00 00 00 00 00 00 00 00 00 00 00 00 dc 07 07 00 ................ 05 00 06 00 0c 00 30 00 2f 00 58 01 0f e5 52 9a ......0...X...R. 01 00 00 00 78 02 00 00 6c 02 00 00 91 16 a2 3d ....x...l....... 9d e3 a6 4d 8a 4b b4 38 77 bc b1 b7 01 00 00 00 ...M.K.8w....... 00 00 00 00 00 00 00 00 00 00 00 00 dc 07 07 00 ................ 05 00 06 00 0c 00 30 00 2f 00 58 01 0f e5 52 9a ......0...X...R.

The resulting output is: "@@", which needs to be saved based on the type (xlsx, csv, txt) provided by the backend and downloaded locally.

In the service file:

public downloadFile() {
const url = 'http://localhost:3300/star';

return this.http.get(url, {
  headers: new HttpHeaders({
    'Authorization': 'Basic ' + "token",
    'Content-Type': 'application/octet-stream',
  }), responseType: 'blob'
}).pipe(
  tap(
    data => console.log('You received data'),
    error => console.log(error)
  )
);

} In the component file:

this.service.downloadFile().subscribe((res) => {

  //res value is : Blob {size: 519, type: "text/html"}

  // Using file-saver to save the file
  saveAs(res, `demo.xlsx`);
}, error => {
  console.log(error);
});

Dealing with potentially large file sizes being sent from the backend, such as a 100 MB xlsx file in binary format, raises concerns on how to handle downloads efficiently on the client side. Any advice or guidance on this matter would be greatly appreciated. Thank you.

Answer №1

/* Remember to update the blob content with your byte[] */
const binaryData = [yourBinaryDataAsAnArrayOrAsAString];
const blob = new Blob(binaryData, {type: "application/octet-stream"});
const fileName = "fileName.fileExtension";

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

XPath query for the initial descendant element of a different Angular module

When working in Angular, I'm faced with the challenge of selecting the first child component of a parent component using specific CSS (SASS) rules. The structure I have is a list of items within one component, where each item is also a component itsel ...

Having difficulty entering text into the Material UI TextField

I am encountering an issue with a button that opens up a Material UI Dialog containing a TextField. However, I am unable to click into the TextField to input any text. Additionally, when clicking on the button to open the Dialog, I receive the error messag ...

What is the process for deleting a token from local storage and displaying the logout page in Next JS?

I developed a Next.js web application and am looking to display a logout page when the token expires, while also removing the expired token from local storage. How can I ensure that this functionality works no matter which page the user visits within the a ...

Data in DynamoDB Local is deleted once the instance is shut down

My app is utilizing DynamoDB Local and I am facing an issue where it keeps deleting all the sample data each time I shut down the instance. This problem seems to be unique as I have not found any similar cases while searching for a solution. I typically ...

Avoid re-rendering the page in Nuxt when adjusting dynamic parameters

My page has two dynamic parameters that trigger the fetch method to run again when the URL params are changed. I want to avoid this behavior. Fetch Method: async fetch() { await this.getFlightResult(); } Get Result Method: async getResult() { th ...

Using Vue to iterate through elements

I am struggling to loop through an array in a Vue component without duplicating the element specified in the 'v-for' directive. I have consulted the official Vue.js API documentation, as well as various online articles, but haven't found a s ...

How can I detect a click event on an SVG element using JavaScript or jQuery?

Currently, I am developing a web application that utilizes SVG. However, I have encountered an issue: I am struggling to add a click event to each element within the SVG using jQuery. The problem arises when attempting to trigger the event; it seems like t ...

What is the best method to initialize a JavaScript function only once on a website that uses AJAX

Currently, I am facing an issue with a javascript function that needs to be contained within the content element rather than in the header. This is due to a dynamic ajax reload process which only refreshes the main content area and not the header section. ...

Using identical variable names for functions in Node.js

I'm feeling a bit puzzled about the following code snippet. Is it possible to create a class in JavaScript like this? module.exports = function testName(params){ testName.test = function(req, res){ //some code here } return testName; } Inste ...

Issue with the exported elements known as 'StatSyncFn'

My build is showing an error that I'm unable to identify the source or reason for. The error message looks like this... Error: node_modules/webpack-dev-middleware/types/index.d.ts:204:27 - error TS2694: Namespace '"fs"' has no expo ...

Removing vacant rows from a table using Jquery in asp.net mvc

In my partial view, I am using BeginCollectionItem to manage the code. <tr> @using (Html.BeginCollectionItem("QuoteLines")) { <td> @Html.HiddenFor(m => m.QuoteID) @Html.HiddenFor(m => m.QuoteLineID) </td> ...

Eliminate using a confirmation popup

My attempts to delete an employee with a confirmation dialog are not successful. I have already implemented a splice method in my service code. The delete function was functioning correctly before adding the confirmation feature, but now that I have upgrad ...

Using async/await with recursion in NodeJS for handling express requests

My code contains a function named GetAllData() that triggers the GetPurchaseData function, which in turn calls itself recursively until all the data is loaded. async function GetAllData(){ console.log("starting loading purchase data"); await G ...

Replacing text with new content when text is stored in a separate file

After organizing all the strings from my code, I compiled them into a file named constants.ts. export class Constants { public static API_URL = '/api/'; public static CREATE_CHILD_API_URL = Constants.API_URL + '%s' + '/create- ...

Blog entries alternating between a pair of distinct hues

I want to create a design where each post container has a different color from the one next to it. Essentially, I would like the containers to alternate between two distinct colors. The left side shows how it currently appears, while the right side depict ...

I'm having trouble asynchronously adding a row to a table using the @angular/material:table schematic

Having trouble asynchronously adding rows using the @angular/material:table schematic. Despite calling this.table.renderRows(), the new rows are not displayed correctly. The "works" part is added to the table, reflecting in the paginator, but the asynchron ...

Odd Behavior when altering button attribute using Jquery

After disabling a button with a click handler, I have noticed an interesting behavior where the button does not submit afterwards. The issue seems to vary between different browsers, with Firefox still allowing form submission after the button is disabled, ...

When using Magento, pasting the same link into the browser produces a different outcome than clicking on window.location - specifically when trying to add items to the

I'm in the process of setting up a Magento category page that allows users to add configurable products directly from the category page, which is not the standard operation for Magento. After putting in a lot of effort, I have almost completed the ta ...

Prevent span/button clicks by graying them out and disabling the ability to click using HTML and JQuery

I am facing a challenge with my two spans that reveal specific information when clicked. I want to make one span appear as a disabled button (greyed out) when the other span is clicked. I tried using $("#toggle-odd").attr("disabled", tr ...

The field 'XXX' is not a valid property on the type 'CombinedVueInstance<Vue, {}, {}, {}, Readonly<Record<never, any>>>'

When creating a Vue component with TypeScript, I encountered an error message in the data() and methods() sections: Property 'xxx' does not exist on type 'CombinedVueInstance<Vue, {}, {}, {}, Readonly<Record<never, any>>>& ...