├── .babelrc ├── .editorconfig ├── .gitignore ├── .npmignore ├── .travis.yml ├── README.md ├── example ├── README.md ├── package.json ├── public │ └── index.html └── src │ └── index.js ├── package.json ├── rollup.config.js └── src ├── base64ToFileOrBlob.js ├── index.js ├── saveFileToBlob.js ├── saveFileToLink.js └── utils.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["react"], 3 | "env": { 4 | "production": { 5 | "presets": ["es2015-rollup"] 6 | }, 7 | "test": { 8 | "presets": ["es2015"], 9 | "plugins": ["istanbul"] 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig help us maintain consistent coding style between different editors. 2 | # 3 | # EditorConfig 4 | # http://editorconfig.org 5 | # 6 | root = true 7 | 8 | [*] 9 | indent_style = space 10 | indent_size = 2 11 | end_of_line = lf 12 | charset = utf-8 13 | trim_trailing_whitespace = true 14 | insert_final_newline = true 15 | 16 | [*.md] 17 | trim_trailing_whitespace = false 18 | 19 | [Makefile] 20 | indent_style = tab 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .nyc_output/ 4 | coverage/ 5 | package-lock.json 6 | dist 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | example 3 | *.log 4 | 5 | # don't ignore .npmignore files 6 | !.npmignore 7 | src 8 | rollup.config.js -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4.0" 4 | - "6.0" 5 | before_install: 6 | - curl -o- -L https://yarnpkg.com/install.sh | bash 7 | - export PATH="$HOME/.yarn/bin:$PATH" 8 | script: "npm test" 9 | after_success: "npm run report-coverage" 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # web-downloadfile 2 | 3 | 文件下载 [web-downloadfile](https://github.com/jiang-jackson/web-downloadfile) 4 | 5 | ## BlogAddress 6 | 7 | [纯前端下载文件](https://www.cnblogs.com/jackson-yqj/p/11321275.html) 8 | 9 | ## Installation 10 | 11 | ```sh 12 | $ npm install web-downloadfile --save 13 | # or 14 | $ yarn add web-downloadfile 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```jsx 20 | import { base64ToFileOrBlob, saveFileToBlob, saveFileToLink } from 'web-downloadfile'; 21 | ``` 22 | 23 | ### 一,base64ToFileOrBlob 24 | 25 | 主要针对图片 base64转blob对象 或 直接下载文件 但是文件也可用 26 | 27 | ```jsx 28 | import { base64ToFileOrBlob } from 'web-downloadfile'; 29 | 30 | let Blob = base64ToFileOrBlob(base64,'',true); 31 | 32 | // or 33 | 34 | base64ToFileOrBlob(base64,'',false); 35 | ``` 36 | 37 | ### 二,saveFileToBlob 38 | 39 | 主要用于文件导出下载 支持大部分文件类型 但是文件类型必传 40 | 41 | ```jsx 42 | import { saveFileToBlob } from 'web-downloadfile'; 43 | 44 | saveFileToBlob(Blob,'test','xlsx'); 45 | ``` 46 | 47 | ### 三,saveFileToLink 48 | 49 | 主要pdf文件链接的下载 因为pdf文件链接在浏览器会直接打开 但是其他文件的链接也可以下载 可监听文件下载进度 50 | 51 | link必须允许跨越访问 否则无法下载 52 | ```jsx 53 | import { saveFileToLink } from 'web-downloadfile'; 54 | 55 | saveFileToLink(link,'test','jpg',fn); 56 | ``` 57 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # web-downloadfile usage example 2 | 3 | Install dependencies and start the example: 4 | 5 | ```sh 6 | npm install 7 | npm start 8 | ``` 9 | 10 | Open http://localhost:3000 to view it in the browser. 11 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web-downloadfile-usage-examples", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "web-downloadfile": "^0.0.1", 7 | "katex": "^0.10.0", 8 | "react": "^16.2.0", 9 | "react-dom": "^16.2.0", 10 | "react-katex-yingqi": "0.0.2", 11 | "react-scripts": "1.1.0" 12 | }, 13 | "scripts": { 14 | "start": "react-scripts start" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /example/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |