Skip to content

Commit

Permalink
feat: add showOpenFilePicker method
Browse files Browse the repository at this point in the history
  • Loading branch information
linxianxi committed Jun 2, 2023
1 parent 0d0ca2f commit 865fd71
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
73 changes: 36 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ online example: https://upload.react-component.vercel.app/

## Feature

* support IE11+, Chrome, Firefox, Safari
- support IE11+, Chrome, Firefox, Safari

## install

Expand All @@ -54,29 +54,29 @@ React.render(<Upload />, container);

### props

|name|type|default| description|
|-----|---|--------|----|
|name | string | file| file param post to server |
|style | object | {}| root component inline style |
|className | string | - | root component className |
|disabled | boolean | false | whether disabled |
|component | "div"|"span" | "span"| wrap component name |
|action| string &#124; function(file): string &#124; Promise&lt;string&gt; | | form action url |
|method | string | post | request method |
|directory| boolean | false | support upload whole directory |
|data| object/function(file) | | other data object to post or a function which returns a data object(a promise object which resolve a data object) |
|headers| object | {} | http headers to post, available in modern browsers |
|accept | string | | input accept attribute |
|capture | string | | input capture attribute |
|multiple | boolean | false | only support ie10+|
|onStart | function| | start upload file |
|onError| function| | error callback |
|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 |
|openFileDialogOnClick | boolean | true | useful for drag only upload as it does not trigger on enter key or click event |
| name | type | default | description |
| --- | --- | --- | --- |
| name | string | file | file param post to server |
| style | object | {} | root component inline style |
| className | string | - | root component className |
| disabled | boolean | false | whether disabled |
| component | "div" | "span" | "span" | wrap component name |
| action | string &#124; function(file): string &#124; Promise&lt;string&gt; | | form action url |
| method | string | post | request method |
| directory | boolean | false | support upload whole directory |
| data | object/function(file) | | other data object to post or a function which returns a data object(a promise object which resolve a data object) |
| headers | object | {} | http headers to post, available in modern browsers |
| accept | string | | input accept attribute |
| capture | string | | input capture attribute |
| multiple | boolean | false | only support ie10+ |
| onStart | function | | start upload file |
| onError | function | | error callback |
| 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 |
| openFileDialogOnClick | boolean | true | useful for drag only upload as it does not trigger on enter key or click event |

#### onError arguments

Expand All @@ -88,31 +88,30 @@ React.render(<Upload />, container);

1. `result`: response body
2. `file`: upload file
3. `xhr`: xhr header, only for modern browsers which support AJAX upload. since
2.4.0

3. `xhr`: xhr header, only for modern browsers which support AJAX upload. since 2.4.0

### customRequest

Allows for advanced customization by overriding default behavior in AjaxUploader. 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`

- `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

showOpenFilePicker() => void: open file picker

## License

rc-upload is released under the MIT license.
4 changes: 4 additions & 0 deletions src/AjaxUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class AjaxUploader extends Component<UploadProps> {
this.reset();
};

showOpenFilePicker = () => {
this.fileInput?.click();
};

onClick = (e: React.MouseEvent<HTMLDivElement> | React.KeyboardEvent<HTMLDivElement>) => {
const el = this.fileInput;
if (!el) {
Expand Down
4 changes: 4 additions & 0 deletions src/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class Upload extends Component<UploadProps> {
this.uploader.abort(file);
}

showOpenFilePicker() {
this.uploader.showOpenFilePicker();
}

saveUploader = (node: AjaxUpload) => {
this.uploader = node;
};
Expand Down

0 comments on commit 865fd71

Please sign in to comment.