├── .gitignore ├── example ├── README.md ├── package.json ├── index.html └── app.js ├── __tests__ └── index-test.js ├── .eslintrc ├── LICENSE.txt ├── package.json ├── CODE_OF_CONDUCT.md ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # React Qiniu Example 2 | 3 | ## How to compile 4 | 5 | > cd example # cd to this directory 6 | > npm install # installs dependencies and runs Browserify 7 | 8 | ## How to run 9 | 10 | Just open `index.html` in your browser 11 | -------------------------------------------------------------------------------- /__tests__/index-test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /*global describe, it, jest, expect*/ 4 | jest.dontMock('../index'); 5 | 6 | describe('Dropzone', function () { 7 | it('changes the state after click', function () { 8 | var React = require('react/addons'); 9 | // var TestUtils = React.addons.TestUtils; 10 | // var Dropzone = require('../index'); 11 | 12 | expect(1).toBe(1); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-qiniu-example", 3 | "private": true, 4 | "version": "0.0.0", 5 | "author": "Yuan He", 6 | "license": "MIT", 7 | "browserify": { 8 | "transform": [ 9 | [ 10 | "babelify" 11 | ] 12 | ] 13 | }, 14 | "scripts": { 15 | "prepublish": "cd .. && npm install react && cd - && browserify --debug app.js > script.js" 16 | }, 17 | "devDependencies": { 18 | "babelify": "^6.1.2", 19 | "browserify": "^10.2.4" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "env": { 4 | "browser": true, 5 | "node": true 6 | }, 7 | "plugins": [ 8 | "react" 9 | ], 10 | "rules": { 11 | "curly": [2, "multi-line"], 12 | "no-shadow": 0, 13 | "no-trailing-spaces": 0, 14 | "no-underscore-dangle": 0, 15 | "no-unused-expressions": 0, 16 | "quotes": [2, "single", "avoid-escape"], 17 | "react/jsx-quotes": 1, 18 | "react/jsx-no-undef": 1, 19 | "react/jsx-uses-react": 1, 20 | "react/jsx-uses-vars": 1, 21 | "react/no-did-mount-set-state": 1, 22 | "react/no-did-update-set-state": 1, 23 | "react/self-closing-comp": 1, 24 | "react/wrap-multilines": 1, 25 | "semi": 2, 26 | "strict": 0 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 LingoChamp Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-qiniu", 3 | "version": "1.5.0", 4 | "description": "A React Component to upload file to Qiniu ", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest", 8 | "lint": "eslint ./; true" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/lingochamp/react-qiniu.git" 13 | }, 14 | "keywords": [ 15 | "react-component", 16 | "react", 17 | "qiniu", 18 | "upload", 19 | "drag", 20 | "drop" 21 | ], 22 | "author": "Yuan He ", 23 | "license": "MIT", 24 | "dependencies": { 25 | "bluebird": "^3.0.0", 26 | "superagent": "^1.2.0", 27 | "superagent-bluebird-promise": "^3.0.2" 28 | }, 29 | "devDependencies": { 30 | "babel-eslint": "^3.1.18", 31 | "babel-jest": "^5.3.0", 32 | "eslint": "^0.23.0", 33 | "eslint-plugin-react": "^2.5.2", 34 | "jest-cli": "^0.4.13", 35 | "react": "^0.14.0", 36 | "react-dom": "^0.14.0" 37 | }, 38 | "jest": { 39 | "scriptPreprocessor": "/node_modules/babel-jest", 40 | "unmockedModulePathPatterns": [ 41 | "/node_modules/react" 42 | ], 43 | "testPathDirs": [ 44 | "" 45 | ] 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion. 6 | 7 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 8 | 9 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 10 | 11 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 12 | 13 | This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 14 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | react-qiniu | React Component to upload file to Qiniu 6 | 61 | 62 | 63 | 64 |
65 |
66 |
67 |
68 |
69 |
70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-qiniu 2 | 3 | React Component to upload file to [Qiniu](http://www.qiniu.com/) 4 | 5 | See Demo: http://lenage.com/react-qiniu/ 6 | 7 | ## Usage 8 | 9 | Just `require('react-qiniu')` and specify an `onDrop` method that accepts an array of dropped files and pass your Qiniu `token` as prop. 10 | 11 | You can also specify a style object to apply to the Drop Zone. 12 | Optionally pass in a size property to configure the size of the Drop Zone. 13 | 14 | ```jsx 15 | var React = require('react'); 16 | var Qiniu = require('react-qiniu'); 17 | 18 | var ReactQiniuDemo = React.createClass({ 19 | getInitialState: function () { 20 | return { 21 | files: [], 22 | token: 'YOUR_UPLOAD_TOKEN', 23 | uploadKey: 'YOUR_CUSTOM_UPLOAD_KEY', // Optional 24 | prefix: 'YOUR_QINIU_KEY_PREFIX' // Optional 25 | }; 26 | }, 27 | 28 | onUpload: function (files) { 29 | // set onprogress function before uploading 30 | files.map(function (f) { 31 | f.onprogress = function(e) { 32 | console.log(e.percent); 33 | }; 34 | }); 35 | }, 36 | 37 | onDrop: function (files) { 38 | this.setState({ 39 | files: files 40 | }); 41 | // files is a FileList(https://developer.mozilla.org/en/docs/Web/API/FileList) Object 42 | // and with each file, we attached two functions to handle upload progress and result 43 | // file.request => return super-agent uploading file request 44 | // file.uploadPromise => return a Promise to handle uploading status(what you can do when upload failed) 45 | // `react-qiniu` using bluebird, check bluebird API https://github.com/petkaantonov/bluebird/blob/master/API.md 46 | // see more example in example/app.js 47 | console.log('Received files: ', files); 48 | }, 49 | 50 | render: function () { 51 | return ( 52 |
53 | 54 |
Try dropping some files here, or click to select files to upload.
55 |
56 |
57 | ); 58 | } 59 | }); 60 | 61 | React.render(, document.body); 62 | ``` 63 | 64 | when upload, we will add a `promise` to file object, see [index.js](https://github.com/lenage/react-qiniu/blob/master/index.js#L68), 65 | so, you can deal with this promise to handle upload status. (do something when success/failure) 66 | 67 | see more in [example/app.js](https://github.com/lenage/react-qiniu/blob/master/example/app.js) 68 | 69 | ## Contributing 70 | 71 | 1. Fork it ( https://github.com/lingochamp/react-qiniu/fork ) 72 | 2. Create your feature branch (`git checkout -b my-new-feature`) 73 | 3. Commit your changes (`git commit -am 'Add some feature'`) 74 | 4. Push to the branch (`git push origin my-new-feature`) 75 | 5. Create a new Pull Request 76 | 77 | ## Thanks 78 | 79 | [@paramaggarwal](https://github.com/paramaggarwal/react-dropzone) 80 | [@mingxinstar](https://github.com/mingxinstar) 81 | 82 | ## License 83 | 84 | MIT 85 | -------------------------------------------------------------------------------- /example/app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import React from '../node_modules/react'; 4 | import Dropzone from '../index'; 5 | 6 | const ReactQiniuExample = React.createClass({ 7 | getInitialState () { 8 | return { 9 | files: [], 10 | token: 'YOUR_QINIU_TOKEN' 11 | }; 12 | }, 13 | 14 | onUpload (files) { 15 | let progresses = {}; 16 | let _this = this; 17 | files.map(function (f) { 18 | f.onprogress = function(e) { 19 | progresses[f.preview] = e.percent; 20 | console.log(e.percent); 21 | _this.setState({progresses: progresses}); 22 | }; 23 | }); 24 | }, 25 | 26 | onDrop (files) { 27 | console.log('Received files: ', files); 28 | // This will not work because onDrop called after uploadFiles, so 29 | // we need a funtion to set hook before call uploadFiles and attach file to function 30 | this.setState({ 31 | files: files 32 | }); 33 | }, 34 | 35 | showFiles () { 36 | if (this.state.files.length <= 0) { 37 | return ''; 38 | } 39 | 40 | var files = this.state.files; 41 | var progresses = this.state.progresses; 42 | let styles = { 43 | width: '600px', 44 | margin: '10px auto' 45 | } 46 | 47 | return ( 48 |
49 |

Dropped files:

50 |
    51 | {[].map.call(files, function (f, i) { 52 | // f is a element of files 53 | // f.uploadPromise => return a Promise to handle uploading status(what you can do when upload failed) 54 | // f.request => return super-agent request with uploading file 55 | var preview = ''; 56 | var progress = progresses && progresses[f.preview] 57 | if (/image/.test(f.type)) { 58 | preview = ; 59 | } else if (/audio/.test(f.type)) { 60 | preview = ; 61 | } 62 | return
  • {preview} {f.name + ' : ' + f.size/1000 + 'KB.'} {(progress || 0) + '% uploaded'}
  • ; 63 | })} 64 |
65 |
66 | ); 67 | }, 68 | 69 | render () { 70 | var styles = { padding: 30}; 71 | var dropZoneStyles = { 72 | margin: '20px auto', 73 | border: '2px dashed #ccc', 74 | borderRadius: '5px', 75 | width: '300px', 76 | height: '200px', 77 | color: '#aaa' 78 | } 79 | var inputStyles = { marginTop: 30, width: 500}; 80 | return ( 81 |
82 | 88 |
Try dropping some files here, or click files to upload.
89 |
90 | {this.showFiles()} 91 |
92 | ) 93 | } 94 | }); 95 | 96 | React.render(, document.getElementById('app')); 97 | 98 | export default ReactQiniuExample; 99 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*global URL */ 2 | 3 | 'use strict'; 4 | var React = require('react'); 5 | var ReactDOM = require('react-dom') 6 | var request = require('superagent-bluebird-promise'); 7 | 8 | var isFunction = function (fn) { 9 | var getType = {}; 10 | return fn && getType.toString.call(fn) === '[object Function]'; 11 | }; 12 | 13 | var ReactQiniu = React.createClass({ 14 | // based on https://github.com/paramaggarwal/react-dropzone 15 | propTypes: { 16 | onDrop: React.PropTypes.func.isRequired, 17 | token: React.PropTypes.string.isRequired, 18 | // called before upload to set callback to files 19 | onUpload: React.PropTypes.func, 20 | size: React.PropTypes.number, 21 | style: React.PropTypes.object, 22 | supportClick: React.PropTypes.bool, 23 | accept: React.PropTypes.string, 24 | multiple: React.PropTypes.bool, 25 | // Qiniu 26 | uploadUrl: React.PropTypes.string, 27 | uploadKey: React.PropTypes.string, 28 | prefix: React.PropTypes.string 29 | }, 30 | 31 | getDefaultProps: function() { 32 | var uploadUrl = 'http://upload.qiniu.com' 33 | if (window.location.protocol === 'https:') { 34 | uploadUrl = 'https://up.qbox.me/' 35 | } 36 | 37 | return { 38 | supportClick: true, 39 | multiple: true, 40 | uploadUrl: uploadUrl 41 | }; 42 | }, 43 | 44 | getInitialState: function() { 45 | return { 46 | isDragActive: false 47 | }; 48 | }, 49 | 50 | onDragLeave: function(e) { 51 | this.setState({ 52 | isDragActive: false 53 | }); 54 | }, 55 | 56 | onDragOver: function(e) { 57 | e.preventDefault(); 58 | e.dataTransfer.dropEffect = 'copy'; 59 | 60 | this.setState({ 61 | isDragActive: true 62 | }); 63 | }, 64 | 65 | onDrop: function(e) { 66 | e.preventDefault(); 67 | 68 | this.setState({ 69 | isDragActive: false 70 | }); 71 | 72 | var files; 73 | if (e.dataTransfer) { 74 | files = e.dataTransfer.files; 75 | } else if (e.target) { 76 | files = e.target.files; 77 | } 78 | 79 | var maxFiles = (this.props.multiple) ? files.length : 1; 80 | 81 | if (this.props.onUpload) { 82 | files = Array.prototype.slice.call(files, 0, maxFiles); 83 | this.props.onUpload(files, e); 84 | } 85 | 86 | for (var i = 0; i < maxFiles; i++) { 87 | files[i].preview = URL.createObjectURL(files[i]); 88 | files[i].request = this.upload(files[i]); 89 | files[i].uploadPromise = files[i].request.promise(); 90 | } 91 | 92 | if (this.props.onDrop) { 93 | files = Array.prototype.slice.call(files, 0, maxFiles); 94 | this.props.onDrop(files, e); 95 | } 96 | }, 97 | 98 | onClick: function () { 99 | if (this.props.supportClick) { 100 | this.open(); 101 | } 102 | }, 103 | 104 | open: function() { 105 | var fileInput = ReactDOM.findDOMNode(this.refs.fileInput); 106 | fileInput.value = null; 107 | fileInput.click(); 108 | }, 109 | 110 | upload: function(file) { 111 | if (!file || file.size === 0) return null; 112 | var key = file.preview.split('/').pop() + '.' + file.name.split('.').pop(); 113 | if (this.props.prefix) { 114 | key = this.props.prefix + key; 115 | } 116 | 117 | if(this.props.uploadKey){ 118 | key = this.props.uploadKey; 119 | } 120 | 121 | var r = request 122 | .post(this.props.uploadUrl) 123 | .field('key', key) 124 | .field('token', this.props.token) 125 | .field('x:filename', file.name) 126 | .field('x:size', file.size) 127 | .attach('file', file, file.name) 128 | .set('Accept', 'application/json'); 129 | if (isFunction(file.onprogress)) { r.on('progress', file.onprogress); } 130 | return r; 131 | }, 132 | 133 | render: function() { 134 | var className = this.props.className || 'dropzone'; 135 | if (this.state.isDragActive) { 136 | className += ' active'; 137 | } 138 | 139 | var style = this.props.style || { 140 | width: this.props.size || 100, 141 | height: this.props.size || 100, 142 | borderStyle: this.state.isDragActive ? 'solid' : 'dashed' 143 | }; 144 | 145 | 146 | return ( 147 | React.createElement('div', {className: className, style: style, onClick: this.onClick, onDragLeave: this.onDragLeave, onDragOver: this.onDragOver, onDrop: this.onDrop}, 148 | React.createElement('input', {style: {display: 'none'}, type: 'file', multiple: this.props.multiple, ref: 'fileInput', onChange: this.onDrop, accept: this.props.accept}), 149 | this.props.children 150 | ) 151 | ); 152 | } 153 | 154 | }); 155 | 156 | module.exports = ReactQiniu; 157 | --------------------------------------------------------------------------------