Skip to content

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

License

Notifications You must be signed in to change notification settings

nourbalaha/file-downloader-js

Repository files navigation

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);

Contribution

Your contribution is welcome! If you find a bug or if you have a suggestion, please fork this repo and make a pull request. Thank you!

License

MIT License

Made with ❤️ in Tokyo © 2024

By Nour Balaha

logo

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published