Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/keqingrong/tiny-save-as
A tiny JavaScript utility library for file saving
https://github.com/keqingrong/tiny-save-as
blob download file-saving save-as
Last synced: about 1 month ago
JSON representation
A tiny JavaScript utility library for file saving
- Host: GitHub
- URL: https://github.com/keqingrong/tiny-save-as
- Owner: keqingrong
- License: mit
- Created: 2018-10-07T11:53:11.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-01-02T08:29:58.000Z (almost 4 years ago)
- Last Synced: 2024-11-07T23:52:57.967Z (about 1 month ago)
- Topics: blob, download, file-saving, save-as
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 11
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tiny-save-as
[![npm version](https://img.shields.io/npm/v/tiny-save-as.svg)](https://www.npmjs.com/package/tiny-save-as)
> A tiny JavaScript utility library for file saving
Live demo available at [here](https://keqingrong.github.io/tiny-save-as/example/).
## Installation
```sh
npm install tiny-save-as
```The CDN build is also available on unpkg:
- [unpkg.com/tiny-save-as/dist/tiny-save-as.umd.js](https://unpkg.com/tiny-save-as/dist/tiny-save-as.umd.js)
- [unpkg.com/tiny-save-as/dist/tiny-save-as.umd.min.js](https://unpkg.com/tiny-save-as/dist/tiny-save-as.umd.min.js)## Usage
```js
// Using ES modules
import savaAs from 'tiny-save-as';// Using ES modules with `import()`
import('https://unpkg.com/tiny-save-as/dist/tiny-save-as.esm.js')
.then(({default: saveAs}) => {
saveAs();
})
.catch((err) => {
console.log(err);
});// Using CommonJS modules
const savaAs = require('tiny-save-as');
```## API
```js
savaAs(blob, filename)
```- **blob**: `Blob` A blob to save.
- **filename**: `string` A file name with a suffix.## Examples
### Text
```js
const str = `# Lorem ipsum
Lorem ipsum dolor sit amet
Consectetur adipiscing elit
`;
const blob = new Blob([str], { type: 'text/markdown' });
saveAs(blob, 'README.md');
```### JSON
```js
const json = {
bool: true,
num: 3.14159,
str: '字符串',
obj: {
foo: 'bar'
}
};
const str = JSON.stringify(json, null, 2) + '\n';
const blob = new Blob([str], { type: 'application/json' });
saveAs(blob, 'blob.json');
```### Canvas
```js
const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 200;
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(100, 100, 50, 0, 2 * Math.PI);
ctx.fillStyle = '#f60';
ctx.fill();// save canvas as PNG image
canvas.toBlob((blob) => {
saveAs(blob, 'circle.png');
});// or sava it as JPEG image
canvas.toBlob((blob) => {
saveAs(blob, 'circle.jpg');
}, 'image/jpeg', 1);
```### SVG
```js
const str = `
`;const blob = new Blob([str], { type: 'image/svg+xml' });
saveAs(blob, 'circle.svg');
```## Browsers support
| [](https://godban.github.io/browsers-support-badges/)IE / Edge | [](https://godban.github.io/browsers-support-badges/)Firefox | [](https://godban.github.io/browsers-support-badges/)Chrome | [](https://godban.github.io/browsers-support-badges/)Safari |
| --------- | --------- | --------- | --------- |
| IE10, IE11, Edge| last 2 versions| last 2 versions| last 2 versions## License
MIT