简体   繁体   English

react-hook-form材料ui文件上传不给FileList

[英]react-hook-form material ui file upload not giving FileList

i have an issue with react hook form and material-ui file uploading when submiting the form i got string path of one file instead of FileList instance我在提交表单时遇到了 react hook 表单和 material-ui 文件上传的问题我得到了一个文件的字符串路径而不是 FileList 实例

        <Controller
          name='attachments'
          control={control}
          defaultValue=''
          render={({ field }) => <input {...field} type='file' multiple />}
        />

       

full code on codesanbox: codesanbox 上的完整代码:

https://codesandbox.io/s/xenodochial-bhaskara-9vo13?file=/src/App.js https://codesandbox.io/s/xenodochial-bhaskara-9vo13?file=/src/App.js

For it to work, you will have to implement your own onChange property.要使其正常工作,您必须实现自己的onChange属性。 You can use the field.onChange callback for this purpose and pass it the file list as an argument.为此,您可以使用field.onChange回调并将文件列表作为参数传递给它。 Here's how it can be done:这是如何做到的:

<Controller
  name="attachments"
  control={control}
  defaultValue=""
  render={({ field }) => (
    <input
      type="file"
      onChange={e => {
        field.onChange(e.target.files);
      }}
      multiple
    />
  )}
/>

Here is the link to the forked source code 这是分叉源代码的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM