Debian Codegraph @

node/react-dropzone/examples/accept/README.md

raw
1 By providing `accept` prop you can make the dropzone accept specific file types and reject the others.
2
3 The value must be an object with a common [MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) as keys and an array of file extensions as values (similar to [showOpenFilePicker](https://developer.mozilla.org/en-US/docs/Web/API/window/showOpenFilePicker)'s types `accept` option).
4 ```js static
5 useDropzone({
6 accept: {
7 'image/png': ['.png'],
8 'text/html': ['.html', '.htm'],
9 }
10 })
11 ```
12
13 For more information see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input.
14
15 ```jsx harmony
16 import React from 'react';
17 import {useDropzone} from 'react-dropzone';
18
19 function Accept(props) {
20 const {
21 acceptedFiles,
22 fileRejections,
23 getRootProps,
24 getInputProps
25 } = useDropzone({
26 accept: {
27 'image/jpeg': [],
28 'image/png': []
29 }
30 });
31
32 const acceptedFileItems = acceptedFiles.map(file => (
33 <li key={file.path}>
34 {file.path} - {file.size} bytes
35 </li>
36 ));
37
38 const fileRejectionItems = fileRejections.map(({ file, errors }) => (
39 <li key={file.path}>
40 {file.path} - {file.size} bytes
41 <ul>
42 {errors.map(e => (
43 <li key={e.code}>{e.message}</li>
44 ))}
45 </ul>
46 </li>
47 ));
48
49 return (
50 <section className="container">
51 <div {...getRootProps({ className: 'dropzone' })}>
52 <input {...getInputProps()} />
53 <p>Drag 'n' drop some files here, or click to select files</p>
54 <em>(Only *.jpeg and *.png images will be accepted)</em>
55 </div>
56 <aside>
57 <h4>Accepted files</h4>
58 <ul>{acceptedFileItems}</ul>
59 <h4>Rejected files</h4>
60 <ul>{fileRejectionItems}</ul>
61 </aside>
62 </section>
63 );
64 }
65
66 <Accept />
67 ```
68
69 ### Browser limitations
70
71 Because of HTML5 File API differences across different browsers during the drag, Dropzone might not be able to detect whether the files are accepted or rejected in Safari nor IE.
72
73 Furthermore, at this moment it's not possible to read file names (and thus, file extensions) during the drag operation. For that reason, if you want to react on different file types _during_ the drag operation, _you have to use_ mime types and not extensions! For example, the following example won't work even in Chrome:
74
75 ```jsx harmony
76 import React from 'react';
77 import {useDropzone} from 'react-dropzone';
78
79 function Accept(props) {
80 const {
81 getRootProps,
82 getInputProps,
83 isDragActive,
84 isDragAccept,
85 isDragReject
86 } = useDropzone({
87 accept: {
88 'image/jpeg': ['.jpeg', '.png']
89 }
90 });
91
92 return (
93 <div className="container">
94 <div {...getRootProps({className: 'dropzone'})}>
95 <input {...getInputProps()} />
96 {isDragAccept && (<p>All files will be accepted</p>)}
97 {isDragReject && (<p>Some files will be rejected</p>)}
98 {!isDragActive && (<p>Drop some files here ...</p>)}
99 </div>
100 </div>
101 );
102 }
103
104 <Accept />
105 ```
106
107 but this one will:
108
109 ```jsx harmony
110 import React from 'react';
111 import {useDropzone} from 'react-dropzone';
112
113 function Accept(props) {
114 const {
115 getRootProps,
116 getInputProps,
117 isDragActive,
118 isDragAccept,
119 isDragReject
120 } = useDropzone({
121 accept: {
122 'image/*': ['.jpeg', '.png']
123 }
124 });
125
126 return (
127 <div className="container">
128 <div {...getRootProps({className: "dropzone"})}>
129 <input {...getInputProps()} />
130 {isDragAccept && (<p>All files will be accepted</p>)}
131 {isDragReject && (<p>Some files will be rejected</p>)}
132 {!isDragActive && (<p>Drop some files here ...</p>)}
133 </div>
134 </div>
135 );
136 }
137
138 <Accept />
139 ```
140
141 ### Notes
142
143 Mime type determination is not reliable across platforms. CSV files, for example, are reported as text/plain under macOS but as application/vnd.ms-excel under Windows. In some cases there might not be a mime type set at all.
144