The issue with downloading all files in Firefox persists when attempting to download multiple files simultaneously due to an anchor click

I am currently facing an issue with downloading multiple files using TypeScript in Angular 6. I am receiving an array of blobs from a web API service.

Here is the service method used to get multiple blobs for downloading:

 private downloadTest(): void {
    this.downloadService$().subscribe((blobs: Blob[]) => {
        blobs.forEach((blob: Blob, index: number) => {
            FileDownloader.startDownload(blob);
        });
    });
}

Within a loop, I am invoking the startDownload method to initiate the file download as shown below:

  export class Downloader {
        public static startDownload(blob: Blob): void {
            blob = new Blob([blob], { type: "type of file" });
            const url = window.URL.createObjectURL(blob);
            const anchor = document.createElement("a");
            document.body.appendChild(anchor);
            anchor.download = "fileName";
            anchor.target = "_blank";
            anchor.href = url;        
            anchor.click();
            document.body.removeChild(anchor);
            window.URL.revokeObjectURL(url);
    }    
}

While this code works well in Chrome, it encounters issues in Firefox where clicking on the anchor element breaks the loop, resulting in some files not being downloaded completely.

Upon removing the line anchor.download = "fileName";, the files can be downloaded in Firefox too, but each file opens in a new tab with a random filename.

Despite trying various solutions, I have been unable to resolve this issue and ensure that each file downloads in a loop with a specific filename.

Any assistance or guidance on this matter would be greatly appreciated.

Best regards, Raj Sharma

Answer №1

After experimenting with various methods to make it work in Firefox, I discovered a solution by increasing the timeout for each download. By doing this, one download is not impacted by another download. Hopefully, this technique can benefit someone else as well.

function initiateDownloadProcess(): void {
    this.downloadService$().subscribe((blobs: Blob[]) => {
 let intervalTimeOut = 0;
                blobs.forEach(function (blob, index): void {
                    setTimeout(function (): void {
                        startDownload(blob, fileInfos[index]);
                    }, intervalTimeOut += 100);
                });
    });
}

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

Interacting with a JavaScript Link in Firefox using Selenium

I am currently trying to retrieve all the comments from the car blog, Jalopnik. Initially, the webpage does not display the comments but rather fetches them using Javascript, showing only the featured ones. In order to access all comments, I have to click ...

Protractor's compatibility with Headless Chrome is hindered on AWS CodeBuild, yet functions successfully when run locally

I am facing an issue with my webpage that requires Google Authentication before moving on to an angular web page. I have created some basic end-to-end tests which are working perfectly fine in Linux using Chrome Headless: The test finds the username fiel ...

Trigger event in App.component.ts after child view initialization in Angular 8 and higher versions

I've been encountering an issue while trying to incorporate HubSpot page tracking into my Angular 8+ application. Currently, I'm tapping into the NavigationEnd event on my router within app.component.ts. The problem arises when each child compone ...

Accessing data from the subscribe method

When attempting to use certain values for graphs, I encountered an issue where the value returned becomes undefined once outside of the subscribe function. I have experimented with putting the values in an array of objects but still believe this is the co ...

Using the scrollIntoView method on an element with a height of 0 within an Angular 2+ application

I am currently working with the following HTML code snippet: <div class="row-20 st-margin" *ngIf="role == 'Administrator'" id="hr-data"> <div class="col-md-12"></div> </div> After ...

Open new tab for Angular OAuth2 OIDC login process

Currently, I am incorporating the authorization code flow using angular-oauth2-oidc in my Angular application. It is a fairly straightforward process. However, I would like to have the ability for the login flow to open in a new tab when the login button ...

The object { production: boolean; } does not include the property 'firebase'

While attempting to deploy my Angular 4 app on both Firebase and Heroku for production, I encountered the following error: ERROR in /Users/.../... (57,49): Property 'firebase' does not exist on type '{ production: boolean; }'. This ...

Verify whether the default export of a file is a React function component or a standard function

Trying to figure out how to distinguish between modules exporting React function components and regular functions. Bun employs file-based routing, enabling me to match requests with module files to dynamically import based on the request pathname. Conside ...

Single jQuery scrolling loader malfunctioning in WebKit browser

I've been conducting an interesting experiment in which new entries are dynamically loaded onto a page as you scroll down. Check out a practical demonstration here. In Firefox, everything seems to be working perfectly. However, when viewed in WebKit ...

Learning to implement the latest language features in JavaScript on older runtimes using TypeScript

Currently, I am faced with a dilemma in my TypeScript project involving the use of flatMap. The issue arises from the fact that this project needs to be compatible with Node.js versions as old as v10, which do not support flatMap. I had assumed that TypeS ...

Modify information in formArray

Let's take a look at this setup I have: Model: export class MapDetailModel{ id: number; lat: number; lon: number; alt: number; long: number; angle: number; distance?: number; pendenza?: number; } Html: <div clas ...

What is the best way to access event.target as an object in Angular 2?

Apologies for my limited English proficiency. . I am trying to write code that will call myFunction() when the user clicks anywhere except on an element with the class .do-not-click-here. Here is the code I have written: document.addEventListener(' ...

React Native - The size of the placeholder dictates the height of a multiline input box

Issue: I am facing a problem with my text input. The placeholder can hold a maximum of 2000 characters, but when the user starts typing, the height of the text input does not automatically shrink back down. It seems like the height of the multiline text ...

Accordion's second child element experiencing issues with grid properties

I have set the parent element display:"Grid" and specified the gridColumnStart for my child elements as shown below. It seems to be working correctly for the first child element, but not for the second one. Please find my code attached: return ( ...

Exploring the weather API integration with Angular 4

I've embarked on a journey to teach myself Angular4 by creating a Weather app. However, I'm facing challenges in connecting the API. Despite consulting various resources, such as: https://medium.com/craft-academy/connecting-an-api-to-an-angular- ...

Retrieve the variable only once a response has been received from the POST request

Is there a way to update a variable in my component only after receiving a response from a POST request? Here is the code in component.ts: formSubmit() { this.sent = this.submitProvider.sendByPost(this.form); this.formSent = this.submitProvider.f ...

Ionic 5 Error: Unable to access 'subscribe' property of undefined object

Working with IONIC 5 service: import { HttpClient } from '@angular/common/http'; . . . getPlaces(placeId: string) { return this.httpClient.get( `https://test.com/offered-place/${placeId}.json`) } . . . Encountering an issue when tryin ...

Angular Recursive and Nested Reactive Form: Ensuring Continuous Validity, Even in Challenging Situations

Currently, I am in the process of developing a recursive Reactive Form using Angular. You can find the working form on STACKBLITZ HERE The functionality of the form is excellent as expected. However, due to its recursive and dynamic nature, where users ca ...

Update to Material-UI 4.8.1 - Is there a different method for defining the `component` property now?

Note: Please note that there was a bug in version 4.8.x which caused this issue. To resolve it, make sure to upgrade to version 4.9.0 or above. In the initial version 4.8.0, the following code would compile and run smoothly: <DialogContent> {/* us ...

Ionic/Angular 2: Issue accessing object in the Page1 class - error caused by: Unable to retrieve the 'name' property as it is undefined

Currently, I am working on developing an application using Ionic and below is the code snippet: <ion-header> <ion-navbar> <button ion-button menuToggle> <ion-icon name="menu"></ion-icon> </button> &l ...