string-to-file
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

string-to-file

Transform string to file in client side.

Playground: https://string-to-file.js-bridge.com

Installation

$ yarn add string-to-file
$ npm install -S string-to-file

playground

git clone https://github.com/jerrywu001/string-to-file

npm i

npm run dev

Usage

  • json to file
import { stringToFile, saveFile } from 'string-to-file';

const json = {
  name: "jack",
  age: 12
};

const jsonFile = stringToFile(JSON.stringify(json, null, 2), 'xxx.json');

// download
saveFile(jsonFile);
  • string to file
import { stringToFile, saveFile } from 'string-to-file';

const string = `Hello world!

How are you!`;

const stringFile = stringToFile(string, 'xxx.txt');

// download
saveFile(stringFile);
  • script to file
import { stringToFile, saveFile } from 'string-to-file';

const jsString = `const a = 'Hello world!';

console.log(a);`;

const jsFile = stringToFile(jsString, 'xxx.js');

// download
saveFile(jsFile);

Utils

  • fileToBlob(file: File): Blob

  • saveFile(file: File, customFileName?: string): Promise<void>

  • blobToString(blob: Blob): Promise<string>

  • fileToString(file: File): Promise<string>

  • jsonToBase64(json: Record<string, any>): Promise<string>

  • stringToBase64(str: string, opts?: { forceEncode: boolean; fileType: FileType; }): string

  • base64ToFile(base64Url: string, filename: string): File

Package Sidebar

Install

npm i string-to-file

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

13.2 kB

Total Files

7

Last publish

Collaborators

  • wc57242263