Skip to content

Commit 865fd71

Browse files
committed
feat: add showOpenFilePicker method
1 parent 0d0ca2f commit 865fd71

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

README.md

+36-37
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ online example: https://upload.react-component.vercel.app/
3636

3737
## Feature
3838

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

4141
## install
4242

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

5555
### props
5656

57-
|name|type|default| description|
58-
|-----|---|--------|----|
59-
|name | string | file| file param post to server |
60-
|style | object | {}| root component inline style |
61-
|className | string | - | root component className |
62-
|disabled | boolean | false | whether disabled |
63-
|component | "div"|"span" | "span"| wrap component name |
64-
|action| string &#124; function(file): string &#124; Promise&lt;string&gt; | | form action url |
65-
|method | string | post | request method |
66-
|directory| boolean | false | support upload whole directory |
67-
|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) |
68-
|headers| object | {} | http headers to post, available in modern browsers |
69-
|accept | string | | input accept attribute |
70-
|capture | string | | input capture attribute |
71-
|multiple | boolean | false | only support ie10+|
72-
|onStart | function| | start upload file |
73-
|onError| function| | error callback |
74-
|onSuccess | function | | success callback |
75-
|onProgress | function || progress callback, only for modern browsers|
76-
|beforeUpload| function |null| before upload check, return false or a rejected Promise will stop upload, only for modern browsers|
77-
|customRequest | function | null | provide an override for the default xhr behavior for additional customization|
78-
|withCredentials | boolean | false | ajax upload with cookie send |
79-
|openFileDialogOnClick | boolean | true | useful for drag only upload as it does not trigger on enter key or click event |
57+
| name | type | default | description |
58+
| --- | --- | --- | --- |
59+
| name | string | file | file param post to server |
60+
| style | object | {} | root component inline style |
61+
| className | string | - | root component className |
62+
| disabled | boolean | false | whether disabled |
63+
| component | "div" | "span" | "span" | wrap component name |
64+
| action | string &#124; function(file): string &#124; Promise&lt;string&gt; | | form action url |
65+
| method | string | post | request method |
66+
| directory | boolean | false | support upload whole directory |
67+
| 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) |
68+
| headers | object | {} | http headers to post, available in modern browsers |
69+
| accept | string | | input accept attribute |
70+
| capture | string | | input capture attribute |
71+
| multiple | boolean | false | only support ie10+ |
72+
| onStart | function | | start upload file |
73+
| onError | function | | error callback |
74+
| onSuccess | function | | success callback |
75+
| onProgress | function | | progress callback, only for modern browsers |
76+
| beforeUpload | function | null | before upload check, return false or a rejected Promise will stop upload, only for modern browsers |
77+
| customRequest | function | null | provide an override for the default xhr behavior for additional customization |
78+
| withCredentials | boolean | false | ajax upload with cookie send |
79+
| openFileDialogOnClick | boolean | true | useful for drag only upload as it does not trigger on enter key or click event |
8080

8181
#### onError arguments
8282

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

8989
1. `result`: response body
9090
2. `file`: upload file
91-
3. `xhr`: xhr header, only for modern browsers which support AJAX upload. since
92-
2.4.0
93-
91+
3. `xhr`: xhr header, only for modern browsers which support AJAX upload. since 2.4.0
9492

9593
### customRequest
9694

9795
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.
9896

9997
customRequest callback is passed an object with:
10098

101-
* `onProgress: (event: { percent: number }): void`
102-
* `onError: (event: Error, body?: Object): void`
103-
* `onSuccess: (body: Object): void`
104-
* `data: Object`
105-
* `filename: String`
106-
* `file: File`
107-
* `withCredentials: Boolean`
108-
* `action: String`
109-
* `headers: Object`
110-
99+
- `onProgress: (event: { percent: number }): void`
100+
- `onError: (event: Error, body?: Object): void`
101+
- `onSuccess: (body: Object): void`
102+
- `data: Object`
103+
- `filename: String`
104+
- `file: File`
105+
- `withCredentials: Boolean`
106+
- `action: String`
107+
- `headers: Object`
111108

112109
### methods
113110

114111
abort(file?: File) => void: abort the uploading file
115112

113+
showOpenFilePicker() => void: open file picker
114+
116115
## License
117116

118117
rc-upload is released under the MIT license.

src/AjaxUploader.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class AjaxUploader extends Component<UploadProps> {
4141
this.reset();
4242
};
4343

44+
showOpenFilePicker = () => {
45+
this.fileInput?.click();
46+
};
47+
4448
onClick = (e: React.MouseEvent<HTMLDivElement> | React.KeyboardEvent<HTMLDivElement>) => {
4549
const el = this.fileInput;
4650
if (!el) {

src/Upload.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class Upload extends Component<UploadProps> {
2929
this.uploader.abort(file);
3030
}
3131

32+
showOpenFilePicker() {
33+
this.uploader.showOpenFilePicker();
34+
}
35+
3236
saveUploader = (node: AjaxUpload) => {
3337
this.uploader = node;
3438
};

0 commit comments

Comments
 (0)