1. A React component of async file uploading, using File API+FormData in modern browser, and form+iframe in IE9-. If want to use in IE8, use es5-shim or so.
2. With help of ES6, so babel is required.
3. When in IE9-, an invisible will be put over the chooseBtn so that it can catch the click event. It is simpler in moderns because the event will be caught by the wrapper.
5. Life circle functions.
6. No preset styles. Just use your favorite.
$3
`` const FileUpload = require('react-fileupload');
...
render(){
/set properties/
const options={
baseUrl:'http://127.0.0.1',
param:{
fid:0
}
}
/Use FileUpload with options/
/Set two dom with ref/
return (
)
}
`
Download ##
npm install react-fileupload --save
API-EN ##
$3
` options:{
baseUrl:'xxx',
...
}
` options is an attribute of . The properties of options are:
name | type | default | note
------------ | ------------- | ------------ | ------------
baseUrl | string | '' | url
param | object | {} | params that appended after baseUrl.
dataType | 'json'/'text' | 'json' | type of response.
chooseAndUpload | boolean | false | whether the upload action will be triggered just after the user choose a file. If true, an DOM with the ref='chooseAndUpload' should be use as a child. default to false.
paramAddToFile(deprecated) | array[string] | [] | an array that including names of params that need to append to the file instance(File ApI instance). default to [].
wrapperDisplay | string | 'inline-block' | the display of the wrappers of chooseBtn or uploadBtn. default to 'inline-block'.
timeout | number | 0 | Timeout of the request, not support IE9- right now, when is timeout the uploadError will be triggered, and an object {type:'TIMEOUTERROR',message:'timeout'} will be return as the argument. default to 0 as no limit.
paramAddToField | object/func | undefined | Key-value that need to add to formData. When it is a function, use the return.
accept | string | '' | Limit the type (extension) of file.
multiple | boolean | false | Allow multi-upload or not. Not supporting IE9-.
numberLimit | number/func | false | Limit how much file that user can choose in multi-upload case.User can still choose but FileUpload will filter.
fileFieldName | string/func | false | When a file is added to formData, defaulting file name as key. When is a string, use it. And When is a func, use return value. Func will receive the File object as argument.
withCredentials | boolean | false | Same as xhr.withCredentials.
requestHeaders | object | false | Key-values that will be set using xhr.setRequestHeader(key, value).
userAgent | string | undefined | Used to set the User Agent String when serverside rendering isomorphic applications. (required when rendering on the server)
$3
Also set as the properties of options.
#### beforeChoose() ####
Triggered after clicking the chooseBtn and before choosing file. return true to continue or false to stop choosing.
@param null
@return {boolean} allow the choose or not
#### chooseFile(files) ####
The callback triggered after choosing.
@param files {array[File] | string} In moderns it will be the array contains the File instance(the way that File API returns). In IE9- it will be the full name of file.
@return
#### beforeUpload(files,mill) ####
Triggered before uploading. return true to continue or false to stop uploading.
@param files {array[File] | string} In moderns it will be the array contains the File instance(the way that File API returns). In IE9- it will be the full name of file.
@param mill {long} The time of the upload action (millisecond). If the File instance has the mill property it will be the same as it.
@return {boolean} Allow the upload action or not.
#### doUpload(files,mill,xhrID) ####
Triggered after the request is sent(xhr send | form submit).
@param files {array[File] | string} In moderns it will be the array contains the File instance(the way that File API returns). In IE9- it will be the full name of file.
@param mill {long} The time of the upload action (millisecond). If the File instance has the mill property it will be the same as it.
@param xhrID {int} ID of this uploading xhr. Could be useful for abort.
@return
#### onabort(mill,id) ####
Triggered after you aborting a xhr.
@param mill {long} The time of the upload action (millisecond) that you aborted.
@param xhrID {int} The ID of the xhr taht you aborted.
#### uploading(progress) ####
It will be triggered continuously when the file is uploading in moderns.
@param progress {Progress} Progress instance,useful properties such as loaded and total can be found.
@return
#### uploadSuccess(resp) ####
Callback when upload succeed (according to the AJAX simply).
@param resp {json | string} The response is fomatted According to options.dataType.
@return
#### uploadError(err) ####
Callback when error occurred (according to the AJAX simply).
@param err {Error | object} If this is an error that caught by try, it will be an object with type and message.
@return
#### uploadFail(resp) ####
Callback when upload failed (according to the AJAX simply).
@param resp {string} Message of it.
$3
Also can be set as property of options, but is not in common use.
#### textBeforeFiles ####
{boolean}
make this true to add text fields before file data.
#### tag ####
{string}
Multi form groups are required in IE. If there are multi-use of in one page, use tag to distinguish them.
#### _withoutFileUpload ####
{boolean}
Send AJAX without the file(without the FormData).
#### disabledIEChoose ####
{boolean | func}
In IE, the upload button is actually covered by an invisible , and the disabled attribute for button will not work. So set this property as true (function return true) to disabled choose behavior.
#### filesToUpload(deprecated) ####
Use filesToUpload(files) of component functions instead.
{array[File]}
IF there is file(File instance) that need to be uploaded immediately, it can be pushed in this array, and should be cleared in beforeUpload or doUpload. Not supporting IE. This file will be detected in componentWillReceiveProps and uploaded.
$3
You can just set two btns.
`
`
Or if you set the chooseAndUpload to true, you need to set only one with ref="chooseAndUpload".
` chooseAndUpload
#### filesToUpload ####
IF there is file(File instance) that need to be uploaded immediately,use this function. BeforeUpload() will be triggered after this function
@param files {array[file]} files array that need to be uploaded
@return null
#### forwardChoose ####
Do the same as clicking chooseBtn . Only support modern browsers.
@param null
@return null
#### abort ####
Abort a xhr. Temporarily only works in modern browsers.
@param xhrID {int} If not passing an ID, will abort the newest one. You can get the ID of a xhr in doUpload().
examples ##
If you have better and clearer demos, plz tell me! Online or offline.
simple example:
` const FileUpload = require('react-fileupload');
...
render(){
/set properties/
const options={
baseUrl:'http://127.0.0.1',
param:{
fid:0
}
}
/Use FileUpload with options/
/Set two dom with ref/
return (
choose upload
- Add special property textBeforeFiles, thanks @Pritoj.
- Add component function abort - Update function doUpload(files, mill, xhrID) - Add life circle function onabort
$3
- Add property withCredentials.
- Add property requestHeaders.
- Add main.min.js, and main in package.json points at that file now.
$3
- Fix #7, unexpected error on fileFIeldName.
$3
- Fix #6, when passing undefined and null as child, a TypeError is raised.
$3
- Update property fileFieldName, can be string or func.
$3
- Fix bug in main.js
- Update dependency react to ^15.0.2
$3
- Update lib (babel6+), not supporting IE8 by default, you can use es5-shim or so to rebuild. - DELETE property paramsAddToFile in options. Just add your params to formData.
- Now supporting multiple upload, add property multiple - Add property numberLimit - Add property accept - Add property fileFieldName - Add special property disabledIEChoose - Add component function forwardChoose - Uploading is now supporting IE9-, but just using inteval to create percentage.
$3
- Add PropTypes
$3
- Fix a bug in 1.1.1, which will throw an error in IE
$3
- Add property timeout - Optimize logic of IE form group
$3
- Add component function filesToUpload - Add special property tag`