How to download a file into a subfolder in the Downloads directory using Angular? [duplicate]

Discussão em 'Angular' iniciado por Jose Vicente, Janeiro 13, 2025.

  1. Jose Vicente

    Jose Vicente Guest

    I’m working on an Angular application, and I have a method to download a file. I already have the blob, so I don’t need a server; everything is handled locally. However, I want the downloaded file to be saved in the Downloads directory but inside a specific subfolder. The name of this subfolder will be passed as a parameter.

    I’ve tried several approaches, but none seem to work as expected. Here is my current code:

    downloadPdf(blob: Blob, fileName: string, folderName: string): void {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = `${folderName}/${fileName}`;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    window.URL.revokeObjectURL(url);
    }


    When I run this, the file downloads into the main Downloads folder, but it doesn’t create or save it inside the specified subfolder. Is there a way to achieve this in Angular or JavaScript?

    What I’ve tried so far:

    • Adding the folderName as part of the download attribute, like in the code above.
    • Looking for options to define the download path in JavaScript, but I couldn’t find anything.

    Notes:

    • The goal is to save the file locally, ideally without requiring additional server interaction.
    • If it’s not possible due to browser limitations, are there any workarounds?

    Any help would be greatly appreciated!

    Continue reading...

Compartilhe esta Página