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

What steps should be taken to transform this Jquery Ajax code into Pure Javascript Fetch?

I am looking to convert this Jquery Ajax snippet to Fetch using Pure Javascript. Can you provide assistance? I attempted this previously, but my code did not function properly. I even posted a question about it here. That is why I am hoping for your help ...

Is it possible to incorporate an editable text feature within react-moveable? (Allowing for both dragging and editing capabilities)

In React Movable box, the text can be dragged with a left click and edited with a right click using the "contentEditable" attribute. However, on smartphones, editing seems to be disabled and only dragging is possible. <div className="move ...

Opt for a line break over a semicolon when using jQuery

I need assistance with breaking the line wherever a semicolon is present. var locdata = { "locations": [{ "id": 0, "name": 'USA', "cx": 100, "cy": 100, "address":'545, 8t ...

Exploring the method of subscribing to streams in Angular2

I have a streaming web-service running on localhost:8080/stream that responds whenever a new message is added to the subscribed mqtt stream. I am looking to integrate this web-service into my Angular2 app using RxJS to consume NodeJS APIs. The code snipp ...

Tips for utilizing the 'crypto' module within Angular2?

After running the command for module installation: npm install --save crypto I attempted to import it into my component like this: import { createHmac } from "crypto"; However, I encountered the following error: ERROR in -------------- (4,28): Canno ...

Steps for logging in using Spring Boot and Angular 2

My Front End application is built with Angular 2 and runs on http:// localhost:5555. Meanwhile, my Back End application uses Spring Boot and runs on http://localhost:8080/. It provides a REST API for my Angular 2 application. Sending requests from http:/ ...

What advantages does CfnAppSync provide over using AppSync in a CDK project?

We are in the process of enhancing our API by adding new JS resolvers and phasing out the VTL resolvers for an AWS AppSync CDK project, specifically built with Cfn<> Cloud Front CDK. The code snippet below illustrates how this can be achieved: ...

Troubleshooting a cross-component property problem involving a fetch request within a subscription

My current objective is to utilize ActivatedRoute parameters to make a fetch request and update a class property with the fetched data. Progress has been made on making the request, but I am facing difficulties in getting the fetched data to the specific c ...

Ways to halt a script entirely once its tag has been eliminated

I have a script from a page tracker website that runs periodically. Occasionally I need to restart the script from the beginning. To do this, I typically remove the script tag from the DOM and then re-append it. However, because the script utilizes setInt ...

What could be the reason behind the login button not triggering the console message display?

I've decided to delve into web server development on my own and have been tweaking a GitHub repository for ExpressJS with Typescript that I stumbled upon. My initial goal is simple - just to have something displayed on the console when I click the log ...

A guide on utilizing ngx-translate to convert validation messages in Ionic4

In my ionic4 application, I am configuring it to support two languages - ar and en using ngx-translate library. I have two JSON files, en.json and ar.json, with the following structure: { "LOGIN": { "login": "Login", "emailOrPhone":"EMAIL OR PHO ...

Is nextJS SSR the optimal solution for handling undefined variables in the window object to enable optional chaining in cases where global variables are not supported?

When using (window?.innerHeight || 420) for server-side rendering (SSR), the translation would be (((_window = window) === null || _window === void 0 ? void 0 : _window.innerHeight) || 420) However, this could result in a referenceError: window is not d ...

Modifying an element in an array while preserving its original position

Currently, I am working on updating the state based on new information passed through response.payload. Here is my existing code snippet: if(response.events.includes('databases.*.collections.*.documents.*.update')) { setMemos(prevState => pre ...

How come only the final element is being displayed from an array in JavaScript, rather than all of the elements present

I am facing an issue while attempting to extract specific information from a JSON data and create a new array with key-value pairs. However, instead of getting all the elements, it only returns the last one. Here is my current code snippet: const input = ...

Need help accessing data from an API using Axios.post and passing an ID?

Can someone help me with passing the ID of each item using Axios.Post in order to display its data on a single page? The image below in my Postman shows how I need to send the ID along with the request. Additionally, I have the first two URL requests for t ...

Sending multiple forms at once with a single submission

I've been racking my brain trying to determine the feasibility of a particular task. I am currently utilizing zen-cart as my shopping cart software, but what I really want to achieve is creating a hardcoded page featuring a list of 7-9 products. Each ...

How can you loop through keys and values in a JSON object using JavaScript?

I am attempting to cycle through the JSON data below: { "VERSION" : "2006-10-27.a", "JOBNAME" : "EXEC_", "JOBHOST" : "Test", "LSFQUEUE" : "45", "LSFLIMIT" : "2006-10-27", "NEWUSER" : "3", "NEWGROUP" : "2", "NEWMODUS" : "640" } The keys in this JSON are d ...

shifting the angular directives to alternate the bootstrap class of hyperlinks

I have a collection of hyperlinks displayed on my webpage. <a href="#" class="list-group-item list-group-item-action active" routerLink='/route1' >Explore First Link</a> <a href="#" class="list-group-item list-group-item-action" r ...

Extract distinct values along with their respective counts from an array containing objects

Upon receiving a JSON request containing an array of objects with two properties, the task at hand is to extract the unique values along with their respective quantities. The following JSON data is being sent through Postman: [ {"name": "First value" ...

Guide to asynchronously loading images with Bearer Authorization in Angular 2 using NPM

I am in search of a recent solution that utilizes Angular2 for my image list. In the template, I have the following: <div *ngFor="let myImg of myImages"> <img src="{{myImg}}" /> </div> The images are stored as an array w ...