POST muiltiple multipart file with React

不羁的心 提交于 2019-12-12 04:37:18

问题


I put this questions in reference POST a file with React.js

I would like now to send a list of files to the endpoint.

My input component:

<input onChange={(e) => Actions.uploadXLS(e.target.files)} multiple type="file" name="xlsz" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" style={{ display: 'none' }}/>

The action handler:

  uploadXLS(files) {
    let i = 0;
    const data = new FormData();
    for ( i = 0; i < files.length; i++ ) {
      data.append( 'file' , files[i]);
    }
    console.log(data);
    this.getInstance().callUploadXLS( data );
  }

console prints: FormData{}


回答1:


        File file = new File("/Users/user1/Documents/VendorDbResources/ExcelFileCore-1.xlsx");
        FileInputStream input = new FileInputStream(file);
        MultipartFile multipartFile1 = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));

        file = new File("/Users/user1/Documents/VendorDbResources/ExcelFileCore-2.xlsx");
        input = new FileInputStream(file);
        MultipartFile multipartFile2 = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));

        MultipartFile[] arrayOfMultipartFile = {multipartFile1, multipartFile2};

        if (arrayOfMultipartFile.length > 0)
            return vendorService.readExcelFile(arrayOfMultipartFile);
        else
            return null;

I have tried backend functionality with static data.above you can see i created an array of multipart file and provided to the service implementation.

And it's working fine. Entries can be seen in db as well.



来源:https://stackoverflow.com/questions/45545647/post-muiltiple-multipart-file-with-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!