从nestjs发送文件

时间:2019-09-05 12:49:27

标签: spring multipartform-data nestjs

我使用react js(frontEnd),nodejs(backend),也使用通过代理与nodejs连接的java spring端点。 我有从nodejs发送formData(file)到spring的问题

我从react js发送,请求主体作为文件的multipart / form-data数组。嵌套js正确。通过http请求,我必须像表单数据一样将其发送到Spring

createApplication(application: any, files: any) {
    var formData = new FormData();
    console.log(files)
    formData.append('application', JSON.stringify(application));

    files.map(file => formData.append('file1', file
    ));
    return this.http.post(
      `${BASIC_URL}/application`,
        formData,
      { headers: formDataHeader },
    );
}

显示错误

TypeError: source.on is not a function
    at Function.DelayedStream.create (/Users/user/workProjects/b2bsc-backend/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
    at FormData.CombinedStream.append (/Users/user/workProjects/b2bsc-backend/node_modules/combined-stream/lib/combined_stream.js:45:37)
    at FormData.append (/Users/user/workProjects/b2bsc-backend/node_modules/form-data/lib/form_data.js:74:3)
    at files.map.file (/Users/user/workProjects/b2bsc-backend/dist/b2b/b2b.service.js:50:36)
    at Array.map (<anonymous>)
    at B2bService.createApplication (/Users/user/workProjects/b2bsc-backend/dist/b2b/b2b.service.js:50:15)
    at B2bController.createApplication (/Users/user/workProjects/b2bsc-backend/dist/b2b/b2b.controller.js:30:32)
    at /Users/user/workProjects/b2bsc-backend/node_modules/@nestjs/core/router/router-execution-context.js:36:29
    at process._tickCallback (internal/process/next_tick.js:68:7)

我想在控制器的Java端获取此文件

   @PostMapping(consumes = { MediaType.MULTIPART_FORM_DATA_VALUE } )
    public
    @ResponseBody
    ResponseEntity<Object> createApplication(@RequestParam(value = "file1") MultipartFile[] files,
                                             @RequestParam String application,
            UriComponentsBuilder ucBuilder) throws IOException {
}```

0 个答案:

没有答案