file-downloader-js

1.0.7 • Public • Published

File Downloader JS

version license downloads

Introduction

File Downloader JS is a simple package that allows you to download a file from the browser

Install with npm

npm install file-downloader-js --save

Install with yarn

yarn add file-downloader-js

Basic usage

Binary download using fetch

import fileDownloader from "file-downloader-js";

function download(url: string, filename: string) {
  fetch(url)
    .then((res) => res.blob())
    .then((blob) => {
      fileDownloader(blob, filename);
    });
}

Binary download using axios

import fileDownloader from "file-downloader-js";
import Axios from "axios";

function download(url: string, filename: string) {
  Axios.get(url, {
    responseType: "blob"
  }).then((res) => {
    fileDownloader(res.data, filename);
  }
}

CSV Data download example

import fileDownloader from "file-downloader-js";
    
let dataRecords = [
  ["ID", "Name", "Age"],
  [1, "Nour", 34],
  [2, "Mila", 1]
];
let data = dataRecords.map((record) => record.join(",")).join("\r\n");
let filename = "data.csv";
let mime = "text/csv";
let bom = new Uint8Array([0xef, 0xbb, 0xbf]);

fileDownloader(data, filename, mime, bom);

License

MIT

Made with ❤️ in Tokyo © 2022

By Nour Balaha

logo

Dependents (0)

Package Sidebar

Install

npm i file-downloader-js

Weekly Downloads

42

Version

1.0.7

License

MIT

Unpacked Size

6.6 kB

Total Files

6

Last publish

Collaborators

  • nourbalaha