save-as-file
v0.4.0
Published
A simple fn to save a file to disk
Readme
save-as-file
A simple fn to save a file to disk.
Table of Contents
Installation
You can install this package from NPM:
npm add save-as-fileOr with Yarn:
yarn add save-as-fileCDN
For CDN, you can use unpkg:
https://unpkg.com/save-as-file/dist/index.min.js
The global namespace for save-as-file is saveAsFile:
<script src="https://unpkg.com/save-as-file/dist/index.min.js"></script>
<script>
const json = JSON.stringify({ping: true});
const file = new File([json], 'test.json', {type: 'application/json'});
saveAsFile(file, 'test.json');
</script>Usage
ES6
Save a File to disk:
import saveFile from 'save-as-file';
const json = JSON.stringify({ping: true});
const file = new File([json], 'test.json', {type: 'application/json'});
saveFile(file, 'test.json');Since a File already carries a name, filename is optional for File input and
defaults to the File's own name:
saveFile(file); // saved as file.nameNOTE: For File/Blob objects we create a temporary object URL which we revoke after 1 minute. If you need to download a large file which may take longer than 1 minute to download, use the 3rd argument to increase this timeout:
import saveFile from 'save-as-file';
const json = JSON.stringify({ping: true});
const file = new File([json], 'test.json', {type: 'application/json'});
saveFile(file, 'test.json', 1000 * 60 * 10 /* 10 mins */);CommonJS
Save a File to disk:
const saveFile = require('save-as-file');
const json = JSON.stringify({ping: true});
const file = new File([json], 'test.json', {type: 'application/json'});
saveFile(file, 'test.json');API
saveFile(data, filename?, gcTimeout?): void| Param | Type | Default | Description |
| ----------- | ------------------------ | ----------- | ----------------------------------------------------------------------------------------- |
| data | File \| Blob \| string | - | The File/Blob to save, or an existing object URL string. |
| filename | string | data.name | Name to save the file as. Optional for File input; defaults to the File's own name. |
| gcTimeout | number | 60000 | Milliseconds to wait before revoking a created object URL. Only used for File/Blob input. |
Browser Support
You can expect this lib to run wherever the download
attribute of an <a> element is supported. See the browserslist field in package.json for
the full support matrix.
Contribute
See AGENTS.md for the full workflow and conventions. In short:
npm ci # install dependencies
npm run lint # oxlint
npm run format # prettier
npm run typecheck # tsc (TypeScript 7)
npm run build # tsdown
npm test # vitest- Use Conventional Commits.
- Every change ships with tests; keep coverage from dropping.
