# wrDownloadBlob

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

Source: https://ngwr.dev/reference/utils/wr-download-blob  
Kind: Util  
Added in v15.5  
Optional

## 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.

```typescript
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.

```angular-ts
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');
});
```

```typescript
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

| Name | Description | Type | Default |
| --- | --- | --- | --- |
| `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` | `-` |
