Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. By providing `accept` prop you can make the dropzone accept specific file types and reject the others.
  2. The value must be a comma-separated list of unique content type specifiers:
  3. * A file extension starting with the STOP character (U+002E). (e.g. .jpg, .png, .doc).
  4. * A valid MIME type with no extensions.
  5. * audio/* representing sound files.
  6. * video/* representing video files.
  7. * image/* representing image files.
  8. For more information see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input
  9. ```jsx harmony
  10. import React from 'react';
  11. import {useDropzone} from 'react-dropzone';
  12. function Accept(props) {
  13. const {acceptedFiles, rejectedFiles, getRootProps, getInputProps} = useDropzone({
  14. accept: 'image/jpeg, image/png'
  15. });
  16. const acceptedFilesItems = acceptedFiles.map(file => (
  17. <li key={file.path}>
  18. {file.path} - {file.size} bytes
  19. </li>
  20. ));
  21. const rejectedFilesItems = rejectedFiles.map(file => (
  22. <li key={file.path}>
  23. {file.path} - {file.size} bytes
  24. </li>
  25. ));
  26. return (
  27. <section className="container">
  28. <div {...getRootProps({className: 'dropzone'})}>
  29. <input {...getInputProps()} />
  30. <p>Drag 'n' drop some files here, or click to select files</p>
  31. <em>(Only *.jpeg and *.png images will be accepted)</em>
  32. </div>
  33. <aside>
  34. <h4>Accepted files</h4>
  35. <ul>
  36. {acceptedFilesItems}
  37. </ul>
  38. <h4>Rejected files</h4>
  39. <ul>
  40. {rejectedFilesItems}
  41. </ul>
  42. </aside>
  43. </section>
  44. );
  45. }
  46. <Accept />
  47. ```
  48. ### Browser limitations
  49. 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.
  50. 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:
  51. ```jsx harmony
  52. import React from 'react';
  53. import {useDropzone} from 'react-dropzone';
  54. function Accept(props) {
  55. const {
  56. getRootProps,
  57. getInputProps,
  58. isDragActive,
  59. isDragAccept,
  60. isDragReject
  61. } = useDropzone({
  62. accept: '.jpeg,.png'
  63. });
  64. return (
  65. <div className="container">
  66. <div {...getRootProps({className: 'dropzone'})}>
  67. <input {...getInputProps()} />
  68. {isDragAccept && (<p>All files will be accepted</p>)}
  69. {isDragReject && (<p>Some files will be rejected</p>)}
  70. {!isDragActive && (<p>Drop some files here ...</p>)}
  71. </div>
  72. </div>
  73. );
  74. }
  75. <Accept />
  76. ```
  77. but this one will:
  78. ```jsx harmony
  79. import React from 'react';
  80. import {useDropzone} from 'react-dropzone';
  81. function Accept(props) {
  82. const {
  83. getRootProps,
  84. getInputProps,
  85. isDragActive,
  86. isDragAccept,
  87. isDragReject
  88. } = useDropzone({
  89. accept: 'image/jpeg, image/png'
  90. });
  91. return (
  92. <div className="container">
  93. <div {...getRootProps({className: "dropzone"})}>
  94. <input {...getInputProps()} />
  95. {isDragAccept && (<p>All files will be accepted</p>)}
  96. {isDragReject && (<p>Some files will be rejected</p>)}
  97. {!isDragActive && (<p>Drop some files here ...</p>)}
  98. </div>
  99. </div>
  100. );
  101. }
  102. <Accept />
  103. ```
  104. ### Notes
  105. Mime type determination is not reliable accross 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.