UtilAdded in v15.5Optional

wrDownloadBlob

Saves a Blob under a file name in one call. Beside it, wrContentDispositionFileName() reads the name a server sent.

Usage

A no-op on the server. The object URL is revoked on a timer, which Safari needs; wr-table exports its CSV through this.

import { wrDownloadBlob } from 'ngwr/utils';

const csv = new Blob(['id,name\r\n1,Ada'], { type: 'text/csv;charset=utf-8;' });
wrDownloadBlob(csv, 'users.csv');

With the server's file name

wrContentDispositionFileName() reads filename* first, then filename, and returns null when the header carries neither.

import { HttpClient } from '@angular/common/http';
import { wrContentDispositionFileName, wrDownloadBlob } from 'ngwr/utils';

this.http.get('/api/report', { observe: 'response', responseType: 'blob' }).subscribe(res => {
  const name = wrContentDispositionFileName(res.headers.get('Content-Disposition'));
  wrDownloadBlob(res.body!, name ?? 'report.pdf');
});
wrContentDispositionFileName('attachment; filename="report.csv"');
// 'report.csv'

wrContentDispositionFileName("attachment; filename=plain.csv; filename*=UTF-8''%E2%82%AC.csv");
// '€.csv': filename* wins, percent-decoded

wrContentDispositionFileName("attachment; filename*=UTF-8''%E2%82; filename=safe.csv");
// 'safe.csv': a broken filename* falls back to filename

wrContentDispositionFileName('inline');
// null

API

NameDescriptionTypeDefault
wrDownloadBlob(blob, fileName)Saves blob under fileName through a temporary anchor. Removes the anchor after the click and revokes the object URL on a timer. Does nothing on the server.(blob: Blob, fileName: string) => void-
wrContentDispositionFileName(header)The file name in a Content-Disposition header, or null. Prefers filename* (percent-decoded) over filename, accepts quoted and unquoted values, ignores parameter case. A filename* that cannot be decoded falls back to filename.(header: string | null | undefined) => string | null-