├── .npmignore ├── .travis.yml ├── .babelrc ├── .eslintrc ├── js-file-download.d.ts ├── .editorconfig ├── package.json ├── README.md ├── LICENSE ├── .gitignore └── file-download.js /.npmignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "8.12.0" 4 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "stage": 0, 3 | "optional": ["runtime"], 4 | "plugins": ["object-assign"] 5 | } -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint:recommended", 3 | "parserOptions": { 4 | "sourceType": "module" 5 | }, 6 | "env": { 7 | "browser": true, 8 | "commonjs": true 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /js-file-download.d.ts: -------------------------------------------------------------------------------- 1 | declare module 'js-file-download' { 2 | export default function fileDownload( 3 | data: string | ArrayBuffer | ArrayBufferView | Blob, 4 | filename: string, 5 | mime?: string, 6 | bom?: string | Uint8Array 7 | ): void; 8 | } 9 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # http://editorconfig.org 4 | 5 | root = true 6 | 7 | [*] 8 | 9 | # Change these settings to your own preference 10 | indent_style = space 11 | indent_size = 4 12 | 13 | # We recommend you to keep these unchanged 14 | end_of_line = lf 15 | charset = utf-8 16 | trim_trailing_whitespace = true 17 | insert_final_newline = true 18 | 19 | [*.md] 20 | trim_trailing_whitespace = false 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "js-file-download", 3 | "version": "0.4.12", 4 | "description": "Javascript function that triggers browser to save javascript-generated content to a file", 5 | "main": "file-download.js", 6 | "scripts": { 7 | "lint": "eslint file-download.js", 8 | "test": "npm run lint" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git@github.com:kennethjiang/js-file-download.git" 13 | }, 14 | "keywords": [ 15 | "javascript", 16 | "file", 17 | "react", 18 | "download" 19 | ], 20 | "author": "Kenneth Jiang ", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/kennethjiang/js-file-download/issues" 24 | }, 25 | "homepage": "https://github.com/kennethjiang/js-file-download", 26 | "typings": "js-file-download.d.ts", 27 | "devDependencies": { 28 | "eslint": "^5.8.0" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Javascript File Download 2 | 3 | Javascript function to trigger browser to save data to file as if it was downloaded. 4 | 5 | # Installation 6 | 7 | npm install js-file-download --save 8 | 9 | # Usage 10 | 11 | ```javascript 12 | var fileDownload = require('js-file-download'); 13 | fileDownload(data, 'filename.csv'); 14 | ``` 15 | 16 | ## Binary downloads 17 | 18 | When downloading binary data, the data must be a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob), otherwise the downloaded file will be corrupted. For example, using [Axios](https://github.com/axios/axios): 19 | 20 | ```javascript 21 | import Axios from axios; 22 | import fileDownload from 'js-file-download'; 23 | 24 | function download(url: string, filename: string) { 25 | Axios.get(url, { 26 | responseType: 'blob', 27 | }).then(res => { 28 | fileDownload(res.data, filename); 29 | }); 30 | } 31 | ``` 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 Kenneth Jiang 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | 30 | # ========================= 31 | # Operating System Files 32 | # ========================= 33 | 34 | # OSX 35 | # ========================= 36 | 37 | .DS_Store 38 | .AppleDouble 39 | .LSOverride 40 | 41 | # Thumbnails 42 | ._* 43 | 44 | # Files that might appear on external disk 45 | .Spotlight-V100 46 | .Trashes 47 | 48 | # Directories potentially created on remote AFP share 49 | .AppleDB 50 | .AppleDesktop 51 | Network Trash Folder 52 | Temporary Items 53 | .apdisk 54 | 55 | # Windows 56 | # ========================= 57 | 58 | # Windows image file caches 59 | Thumbs.db 60 | ehthumbs.db 61 | 62 | # Folder config file 63 | Desktop.ini 64 | 65 | # Recycle Bin used on file shares 66 | $RECYCLE.BIN/ 67 | 68 | # Windows Installer files 69 | *.cab 70 | *.msi 71 | *.msm 72 | *.msp 73 | 74 | # Windows shortcuts 75 | *.lnk 76 | 77 | # IDEA 78 | # ========================= 79 | 80 | .idea 81 | 82 | .publish 83 | -------------------------------------------------------------------------------- /file-download.js: -------------------------------------------------------------------------------- 1 | module.exports = function(data, filename, mime, bom) { 2 | var blobData = (typeof bom !== 'undefined') ? [bom, data] : [data] 3 | var blob = new Blob(blobData, {type: mime || 'application/octet-stream'}); 4 | if (typeof window.navigator.msSaveBlob !== 'undefined') { 5 | // IE workaround for "HTML7007: One or more blob URLs were 6 | // revoked by closing the blob for which they were created. 7 | // These URLs will no longer resolve as the data backing 8 | // the URL has been freed." 9 | window.navigator.msSaveBlob(blob, filename); 10 | } 11 | else { 12 | var blobURL = (window.URL && window.URL.createObjectURL) ? window.URL.createObjectURL(blob) : window.webkitURL.createObjectURL(blob); 13 | var tempLink = document.createElement('a'); 14 | tempLink.style.display = 'none'; 15 | tempLink.href = blobURL; 16 | tempLink.setAttribute('download', filename); 17 | 18 | // Safari thinks _blank anchor are pop ups. We only want to set _blank 19 | // target if the browser does not support the HTML5 download attribute. 20 | // This allows you to download files in desktop safari if pop up blocking 21 | // is enabled. 22 | if (typeof tempLink.download === 'undefined') { 23 | tempLink.setAttribute('target', '_blank'); 24 | } 25 | 26 | document.body.appendChild(tempLink); 27 | tempLink.click(); 28 | 29 | // Fixes "webkit blob resource error 1" 30 | setTimeout(function() { 31 | document.body.removeChild(tempLink); 32 | window.URL.revokeObjectURL(blobURL); 33 | }, 200) 34 | } 35 | } 36 | --------------------------------------------------------------------------------