https://github.com/jonluca/browser-file-download
Download files using this package
https://github.com/jonluca/browser-file-download
Last synced: about 1 year ago
JSON representation
Download files using this package
- Host: GitHub
- URL: https://github.com/jonluca/browser-file-download
- Owner: jonluca
- Created: 2022-08-02T00:02:56.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-02T02:10:43.000Z (almost 4 years ago)
- Last Synced: 2025-03-11T01:37:44.029Z (over 1 year ago)
- Language: TypeScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Browser File Download
Download files with javascript
```javascript
import axios from "axios";
import { download } from "browser-file-downloader";
const download = async (url: string, filename: string) => {
const { data } = await axios.get(url, {
responseType: "blob",
});
download(data, { filename });
};
```
## Options
```typescript
interface DownloadOptions {
filename: string; // filename
mime?: string; // mime time (default: application/octet-stream) - required when using useDataUrl
bom?: BlobPart; // bom (bytes to include at the start of the file)
useDataUrl?: boolean; // whether to use the legacy data url instead of a blob url
}
```
## Examples
#### CSV
If your CSVs are looking weird, you can try
```typescript
import { download } from "browser-file-downloader";
let csvString = "";
const bom = "\uFEFF";
download(csvString, { filename: "data.csv", bom, useDataUrl: true, mime: "text/csv; charset=utf-8" });
```
### Legacy
```javascript
const download = require("browser-file-downloader/dist/cjs/index.legacy.js");
download("My data", { filename: "file.txt" });
```
### Modern ESM
```javascript
import { download } from "browser-file-downloader/dist/mjs/index.modern.js";
download("My data", { filename: "file.txt" });
```