New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

fs-browsers

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-browsers

Usefull files functions for browsers and client side

latest
Source
npmnpm
Version
2.1.1
Version published
Maintainers
1
Created
Source

Fs Browsers

An easy to use files functions and api for browsers.
In your client side code, you can easilly download any file by a url or even export anything you want to several types of files like txt, js, yml, csv, and even excel files.

GitHub package.json version (branch) GitHub issues GitHub GitHub repo file count npm bundle size

Whats New?

  • Add sheet title option to add title in the top of the sheet
  • Add option to override the default excel cells styles

Install

npm install fs-browsers

Download File

downloadFile(url, filename?)
Download file based on url by the following code:

import { downloadFile } from 'fs-browsers';
// ...
downloadFile('url-to-file')

You can set the downloaded file name to anything you want like this:

import { downloadFile } from 'fs-browsers';
// ...
downloadFile('url-to-file', 'my-file.txt')

In some browsers the file name change might not work, read about HTTP Header Content-Disposition

Parameters

ParameterTypeDescriptionRequiredDefault
urlstringthe url for downloading the wanted filetrue-
fileNamestringthe name of the file which will be downloadedfalse-

Export File

exportFile(data, fileName?)

Export data or any text to a file using the following code:

import { exportFile } from 'fs-browsers';
//...
exportFile("this string will be exported to txt file");

The default file type is 'txt' but you can export to any file you want like this:

import { exportFile } from 'fs-browsers';
//...
exportFile("this string will be exported to js file", 'file-new-name.js');

Parameters

ParameterTypeDescriptionRequiredDefault
textstringthe text that will be exported to the filetrue-
fileNamestringthe name of the file which will be exported, including the suffixfalsefile.txt

exportCsvFile(data, fileName, headings?)

To export data to csv file use the following code:

import { exportCsvFile } from 'fs-browsers';
// ...
const data = [
    {
      firstName: 'John',
      lastName: 'Doe',
      age: 20,
    },
    {
      firstName: 'Peter',
      lastName: 'Parker',
      age: 30,
    },
    {
      firstName: 'Mark',
      lastName: 'Twain',
      age: 40,
    },
  ]
exportCsvFile(data);

You can override the csv headings by giving a headings list:

import { exportCsvFile } from 'fs-browsers';
// ...
const data = [
    {
      firstName: 'John',
      lastName: 'Doe',
      age: 20,
    },
    {
      firstName: 'Peter',
      lastName: 'Parker',
      age: 30,
    },
    {
      firstName: 'Mark',
      lastName: 'Twain',
      age: 40,
    },
  ]
const headings = ["First Name", "Last Name", "Age"];
exportCsvFile(data, 'name.csv', headings);

Parameters

ParameterTypeDescriptionRequiredDefault
data{}[]the array of data that will be exported into the Csv filetrue-
fileNamestringthe name of the Csv which will be exported, including the suffixfalsefile.csv
headingsstring[]array of string for the Csv headings rowfalsenull

exportXlsxFile(data, fileName, options?)

To export data to Excel file ('xlsx') use the following code:

import { exportXlsxFile } from 'fs-browsers';
// ...
const data = [
    {
      firstName: 'John',
      lastName: 'Doe',
      age: 20,
    },
    {
      firstName: 'Peter',
      lastName: 'Parker',
      age: 30,
    },
    {
      firstName: 'Mark',
      lastName: 'Twain',
      age: 40,
    },
  ]
exportXlsxFile(data);

You can override the excel headings by giving a headings list:

import { exportXlsxFile } from 'fs-browsers';
const data = [
    {
      firstName: 'John',
      lastName: 'Doe',
      age: 20,
    },
    {
      firstName: 'Peter',
      lastName: 'Parker',
      age: 30,
    },
    {
      firstName: 'Mark',
      lastName: 'Twain',
      age: 40,
    },
  ]
const headings = ["First Name", "Last Name", "Age"];
exportXlsxFile(data, 'names.xlsx', { headings: headings });

You can add title to the excel sheet that will be presented above the data table:

import { exportXlsxFile } from 'fs-browsers';
const data = [
    {
      firstName: 'John',
      lastName: 'Doe',
      age: 20,
    },
    {
      firstName: 'Peter',
      lastName: 'Parker',
      age: 30,
    },
    {
      firstName: 'Mark',
      lastName: 'Twain',
      age: 40,
    },
  ]
const headings = ["First Name", "Last Name", "Age"];
const title = "People";
exportXlsxFile(data, 'names.xlsx', { headings: headings, sheetTitle: title });

It is very much the same as csv files in the code, but the result is a bit different. The Excel file has some simple design and the csv file has not.
Moreover, xlsx files are more complex and functional then csv files.

The Excel file with the title looks like -Excel With Title Example

Parameters

ParameterTypeDescriptionRequiredDefault
data{}[]the array of data that will be exported into the Excel filetrue-
fileNamestringthe name of the Excel which will be exported, including the suffixfalsefile.xlsx
optionsExcelOptionsthe advanced options of the Excel filefalsenull

Options

OptionTypeDescriptionRequiredDefault
headingsstring[]array of string for the Excel headings rowfalsenull
sheetNamestringthe name of the sheet in the ExcelfalseSheet1
sheetTitlestringtitle to be prensented in the top sheet above the data tablefalse-
cellStyleCellStylestyle object to the data table cells as described in the xlsx-js-style packagefalsedefaultCellStyle
headingStyleCellStylestyle object to the data table heading cells as described in the xlsx-js-style packagefalsedefaultHeadingStyle
titleStyleCellStylestyle object to the sheet title cells as described in the xlsx-js-style packagefalsedefaultTitleStyle

License

MIT

Keywords

javascript

FAQs

Package last updated on 17 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts