Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom request option #53

Merged
merged 2 commits into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ React.render(<Upload />, container);
|onSuccess | function | | success callback |
|onProgress | function || progress callback, only for modern browsers|
|beforeUpload| function |null| before upload check, return false or a rejected Promise will stop upload, only for modern browsers|
|customRequest | function | null | provide an override for the default xhr behavior for additional customization|
|withCredentials | boolean | false | ajax upload with cookie send |

#### onError arguments
Expand All @@ -84,6 +85,23 @@ React.render(<Upload />, container);
2. `file`: upload file


### customRequest

Allows for advanced customization by overriding default behavior in AjaxUplaoder. Provide your own XMLHttpRequest calls to interface with custom backend processes or interact with AWS S3 service through the aws-sdk-js package.

customRequest callback is passed an object with:

* `onProgress: (event: { percent: number }): void`
* `onError: (event: Error, body?: Object): void`
* `onSuccess: (body: Object): void`
* `data: Object`
* `filename: String`
* `file: File`
* `withCredentials: Boolean`
* `action: String`
* `headers: Object`


### methods

abort(file?: File) => void: abort the uploading file
Expand Down
4 changes: 3 additions & 1 deletion src/AjaxUploader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { PropTypes } from 'react';
import classNames from 'classnames';
import request from './request';
import defaultRequest from './request';
import getUid from './uid';

const AjaxUploader = React.createClass({
Expand All @@ -22,6 +22,7 @@ const AjaxUploader = React.createClass({
]),
headers: PropTypes.object,
beforeUpload: PropTypes.func,
customRequest: PropTypes.func,
withCredentials: PropTypes.bool,
},

Expand Down Expand Up @@ -111,6 +112,7 @@ const AjaxUploader = React.createClass({
data = data(file);
}
const { uid } = file;
const request = props.customRequest || defaultRequest;
this.reqs[uid] = request({
action: props.action,
filename: props.name,
Expand Down
2 changes: 2 additions & 0 deletions src/Upload.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Upload = React.createClass({
multiple: PropTypes.bool,
disabled: PropTypes.bool,
beforeUpload: PropTypes.func,
customRequest: PropTypes.func,
onReady: PropTypes.func,
withCredentials: PropTypes.bool,
supportServerRender: PropTypes.bool,
Expand All @@ -47,6 +48,7 @@ const Upload = React.createClass({
supportServerRender: false,
multiple: false,
beforeUpload: null,
customRequest: null,
withCredentials: false,
};
},
Expand Down