Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用springBoot和ReactJs通过文件上传添加额外数据_Reactjs_Spring Boot_Visual Studio Code_Axios - Fatal编程技术网

使用springBoot和ReactJs通过文件上传添加额外数据

使用springBoot和ReactJs通过文件上传添加额外数据,reactjs,spring-boot,visual-studio-code,axios,Reactjs,Spring Boot,Visual Studio Code,Axios,希望你们都很好,我在spring boot中上传一个包含一些额外数据的文件时遇到了一个严重问题,这是我的上传文件java代码 // Add new AssignmentList List Record @PostMapping("/uploadfile") public ResponseEntity<String> uploadData(@RequestParam("document") MultipartFile file)

希望你们都很好,我在spring boot中上传一个包含一些额外数据的文件时遇到了一个严重问题,这是我的上传文件java代码

// Add new AssignmentList List Record
    @PostMapping("/uploadfile")
    public ResponseEntity<String> uploadData(@RequestParam("document") MultipartFile file) throws Exception {
        try {
            String fileName = StringUtils.cleanPath(file.getOriginalFilename());
            AssignmentList  assignmentList = new AssignmentList();
            assignmentList.setFileName(fileName);
            assignmentList.setData(file.getBytes());
            assignmentList.setFileSize(file.getSize());
            assignmentList.setFileType(file.getContentType());
            assignmentList.setUploadTime(new Date());
//            assignmentList.setSemester(assignmentList.getSemester());

            assignmentListDao.save(assignmentList);
            return new ResponseEntity("File Uploaded Successfully", HttpStatus.OK);

        }catch (Exception err){
            return new ResponseEntity<>("File not uploaded", HttpStatus.BAD_REQUEST);
        }
    }
分配列表实体如下所示:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name="assignment_list")
public class AssignmentList implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int fileId;

    @Column(name="file_name", length=512, nullable = false, unique = true)
    private String fileName;

    @Column(name="file_size")
    private Long fileSize;

    @Column(name="file_type")
    private String fileType;

    @Column(name="upload_time")
    private Date uploadTime;

    @Lob
    private byte[] data;

    @OneToOne(cascade = {CascadeType.DETACH}, fetch = FetchType.EAGER   )
    private Semester semester;

// constructor
    public AssignmentList(int fileId, String fileName, Long fileSize, Date uploadTime) {
        this.fileId = fileId;
        this.fileName = fileName;
        this.fileSize = fileSize;
        this.uploadTime = uploadTime;
    }
}
当我尝试使用Reactjs上载文件时,spring boot中有以下实体,它将上载该文件,但没有
学期id
,react axios post请求为

const uploadFile = async () => {
        const formData = new FormData();
        formData.append("document", file);
        formData.append("semester", sem);

        await axios
            .post("http://localhost:8080/assignmentlist/uploadfile", formData, {
                headers: {
                    "Content-Type": "multipart/form-data",
                    Accept: "application/json",
                    type: "formData",
                },
            })
            .then(
                function (response) {
                    //handle success
                    console.log(response);
                },
                function (error) {
                    // handle error
                    alert(error.message);
                }
            );
    };
其中
sem
是从其中一个选择框获得的值,即
onChange(e=>setSem(e.target.value))

我希望我澄清我的疑虑
提前感谢:)将控制器的方法签名更改为:

public ResponseEntity<String> uploadData(@RequestParam("document") MultipartFile file, @RequestParam("semester") Semester semester) throws Exception
如果这不起作用,只需将
学期
作为
int
阅读,然后自己从中构建
学期

public ResponseEntity<String> uploadData(@RequestParam("document") MultipartFile file, @RequestParam("semester") Semester semester) throws Exception
assignmentList.setSemester(semester);