Downloading a file in Angular

 First, we instruct the http client to return a blob:

let subs = this.httpClient.get(query, {  responseType: 'blob' })

Then:

subs.subscribe({
      next: (downloadedFile) => {
        const a = document.createElement('a');
        a.setAttribute('style', 'display:none;');
        document.body.appendChild(a);
        a.href = URL.createObjectURL(downloadedFile);
        a.target = '_blank';
        a.click();
        document.body.removeChild(a);
      }
}

Previously in .NET core we should return a File result:

return File(content, contentType, fileName);

Post a Comment

Previous Post Next Post