Giter Site home page Giter Site logo

tiny-save-as's Introduction

tiny-save-as

npm version

A tiny JavaScript utility library for file saving

Live demo available at here.

Installation

npm install tiny-save-as

The CDN build is also available on unpkg:

Usage

// 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

savaAs(blob, filename)
  • blob: Blob A blob to save.
  • filename: string A file name with a suffix.

Examples

Text

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

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

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

const str = `<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40" stroke-width="1" fill="#f60" />
</svg>`;

const blob = new Blob([str], { type: 'image/svg+xml' });
saveAs(blob, 'circle.svg');

Browsers support

IE / Edge
IE / Edge
Firefox
Firefox
Chrome
Chrome
Safari
Safari
IE10, IE11, Edge last 2 versions last 2 versions last 2 versions

License

MIT

tiny-save-as's People

Contributors

keqingrong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

deawx scartf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.