将文件发送到api并在控制器中获取null

时间:2018-08-09 11:16:05

标签: jquery spring-boot

我将文件发送到api如下

const formData = new FormData();
    if ($.isEmptyObject(this.state.attachment)===false) {
        formData.append('attachment', this.state.attachment);
        $.ajax({
            type: 'POST',
            url: process.env.REACT_APP_API_HOST + "/api/attachment",
            async: false,
            timeout: 5000,
            processData: false,
            contentType: false,
            cache: false,
            data: {
                attachment: formData
            }
        });
    }

和附件,我可以通过以下方式

<input type='file' name='attachment' className="form-control" defaultValue={this.state.attachment}
                   onChange={this.handleChange}/>

还有我的控制器

@RequestMapping(value = "/attachment", method = RequestMethod.POST)
public HttpEntity attachment(@ModelAttribute MultipartFile attachment){
    System.out.println(attachment);
    return ResponseEntity.ok().build();
}

我得到的结果为空 我在做什么错?

更新

    handleChange = (event) => {
    this.setState({[event.target.name]: event.target.value});
};

0 个答案:

没有答案