file-input.mdx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. ---
  2. sidebar_position: 3
  3. slug: /file-input
  4. ---
  5. import Tabs from '@theme/Tabs';
  6. import TabItem from '@theme/TabItem';
  7. import UppyCdnExample from '/src/components/UppyCdnExample';
  8. # File input
  9. The `@uppy/file-input` plugin is the most barebones UI for selecting files — it
  10. shows a single button that, when clicked, opens up the browser’s file selector.
  11. ## When should I use it?
  12. When you want users to select files from their local machine with a minimal UI.
  13. ## Install
  14. <Tabs>
  15. <TabItem value="npm" label="NPM" default>
  16. ```shell
  17. npm install @uppy/file-input
  18. ```
  19. </TabItem>
  20. <TabItem value="yarn" label="Yarn">
  21. ```shell
  22. yarn add @uppy/file-input
  23. ```
  24. </TabItem>
  25. <TabItem value="cdn" label="CDN">
  26. <UppyCdnExample>
  27. {`
  28. import { Uppy, FileInput } from "{{UPPY_JS_URL}}"
  29. const uppy = new Uppy()
  30. uppy.use(FileInput, { target: document.body })
  31. `}
  32. </UppyCdnExample>
  33. </TabItem>
  34. </Tabs>
  35. ## Use
  36. ```js showLineNumbers
  37. import Uppy from '@uppy/core';
  38. import FileInput from '@uppy/file-input';
  39. import '@uppy/core/dist/style.min.css';
  40. import '@uppy/file-input/dist/style.css';
  41. new Uppy().use(FileInput, { target: '#uppy-file-input' });
  42. ```
  43. :::note
  44. The `@uppy/file-input` plugin includes some basic styles for use with the
  45. [`pretty`](#pretty-true) option, like shown in the
  46. [example](/examples/xhrupload). You can also choose not to use it and provide
  47. your own styles instead.
  48. Import general Core styles from `@uppy/core/dist/style.css` first, then add the
  49. File Input styles from `@uppy/file-input/dist/style.css`. A minified version is
  50. also available as `style.min.css` at the same path. The way to do import depends
  51. on your build system.
  52. :::
  53. ## API
  54. ### Options
  55. #### `id`
  56. A unique identifier for this plugin (`string`, default: `'FileInput'`).
  57. #### `title`
  58. Configures the title / name shown in the UI, for instance, on Dashboard tabs
  59. (`string`, default: `'File Input'`).
  60. #### `target`
  61. DOM element, CSS selector, or plugin to place the audio into (`string` or
  62. `Element`, default: `null`).
  63. #### `pretty`
  64. When true, display a styled button that, when clicked, opens the file selector
  65. UI. When false, a plain old browser `<input type="file">` element is shown
  66. (`boolean`, default: `true`).
  67. #### `inputName`
  68. The `name` attribute for the `<input type="file">` element (`string`, default:
  69. `'files[]'`).
  70. #### `locale`
  71. ```js
  72. export default {
  73. strings: {
  74. // The same key is used for the same purpose by @uppy/robodog's `form()` API, but our
  75. // locale pack scripts can't access it in Robodog. If it is updated here, it should
  76. // also be updated there!
  77. chooseFiles: 'Choose files',
  78. },
  79. };
  80. ```
  81. ## Custom file inpt
  82. If you don’t like the look/feel of the button rendered by `@uppy/file-input`,
  83. feel free to forgo the plugin and use your own custom button on a page, like so:
  84. ```html
  85. <input type="file" id="my-file-input" />
  86. ```
  87. Then add this JS to attach it to Uppy:
  88. ```js
  89. const uppy = new Uppy(/* ... */);
  90. const fileInput = document.querySelector('#my-file-input');
  91. fileInput.addEventListener('change', (event) => {
  92. const files = Array.from(event.target.files);
  93. files.forEach((file) => {
  94. try {
  95. uppy.addFile({
  96. source: 'file input',
  97. name: file.name,
  98. type: file.type,
  99. data: file,
  100. });
  101. } catch (err) {
  102. if (err.isRestriction) {
  103. // handle restrictions
  104. console.log('Restriction error:', err);
  105. } else {
  106. // handle other errors
  107. console.error(err);
  108. }
  109. }
  110. });
  111. });
  112. // Clear the `<input>` after the upload or when the file was removed
  113. // so the file can be uploaded again (see
  114. // https://github.com/transloadit/uppy/issues/2640#issuecomment-731034781).
  115. uppy.on('file-removed', () => {
  116. fileInput.value = null;
  117. });
  118. uppy.on('complete', () => {
  119. fileInput.value = null;
  120. });
  121. ```