小编fa_*_*a__的帖子

使用react-hook-form上传并保存文件列表

我正在构建一个主要针对使用的浏览器的网站Ionic React。我正在尝试使用 areact-hook-form上传文件列表(以及其他数据)并将它们与其他数据一起保存在 FieldArray 中。

我已经按照Ionic Forum 中的这个答案,使用 IonButton 和输入实现了文件上传。

 <input
       type="file"
       id="file-upload"
       style={{ display: "none" }}
       onChange={() => { setFile(index);}}/>
  <IonButton
       onClick={() => {openFileDialog();}}
       disabled={files === undefined || files[index] === undefined}>
     <IonLabel>Upload file</IonLabel>
     <IonIcon slot="start" />
   </IonButton>
Run Code Online (Sandbox Code Playgroud)

代码:

function openFileDialog() {
    (document as any).getElementById("file-upload").click();
  }

const setFile = (index: number) => (_event: any) => {
    console.log(`Getting file for index ${index}`);

    let f = _event.target.files![0];

    var reader = new FileReader();

    reader.onload = function () { …
Run Code Online (Sandbox Code Playgroud)

reactjs ionic-framework react-hook-form

1
推荐指数
1
解决办法
7047
查看次数

标签 统计

ionic-framework ×1

react-hook-form ×1

reactjs ×1