V5NEXT
V5NEXT

Reputation: 2077

Enable download of file from server angular

I have already found a solution for csv files using blob but it doesn't seem to work with other files, I want to download the file regardless of the type. I will get the the url for the file on the page load,but using blob to preferred the click function is not working.

  CopyUrl(urls) {
    const a = document.createElement("a");
    document.body.appendChild(a);
    const blob: any = new Blob([urls], {
      type: "octet/stream"
    });
    const url = window.URL.createObjectURL(blob);
    a.href = url;
    const data = "test" + ".mp4";
    a.download = data;
    a.click();
    window.URL.revokeObjectURL(url);
    return blob;
  }
     <a class="dropdown-item"
                              (click)="CopyUrl(mediaFiles.data[activeFileIndex].mediaFileResponse.sourceUrl)">Copy Url


                            </a>

Any help would be appreciated.

Upvotes: 2

Views: 292

Answers (1)

Adrita Sharma
Adrita Sharma

Reputation: 22213

You can use file-saver. Send response as a byte stream from your backend and on button Click , make the following request

 this.http.get(url,{ responseType: 'blob' }).subscribe((respBlob: any) => {
    saveAs(respBlob, `file.pdf`)
 });

Upvotes: 2

Related Questions