请求被拒绝,因为未找到多部分边界(Angular +Spring

pdkcd3nj  于 2022-10-30  发布在  Spring
关注(0)|答案(3)|浏览(150)

{"header":{"type":"auto_translation","ret_code":"error","time_cost":627.0,"request_id":"8f35a331585e11ed8b7ea7989c294558"},"message":"Translation error (20001), please retry later. Detail: RuntimeException - The length of source sentence is too long!!! - {\n "header": {\n "time_cost": 0.000412,\n "type": "auto_translation",\n "ret_code": "The length of source sentence is too long!!!"\n }\n}"}

jbose2ul

jbose2ul1#

试试这个,

const formData = new FormData();
 formData.append("file", file);
 formData.append("reportProgress", true);

使用HTTP客户端,

return this.httpclient.post(this.urlUpload, formData);
wz8daaqr

wz8daaqr2#

是否设置了内容类型?如果设置了,请将其删除。

headers:{
        "Content-Type":"multipart/form-data", // remove it
},

前端:

const formData = new FormData();
    formData.append("file", file);
    formData.append("reportProgress", "true");

return this.http.post<void>(this.API_URL + '/upload', formData);

后端:

@Operation(description = "Upload File")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "File Uploaded successfully"),
            @ApiResponse(responseCode = "400", description = "Problem during file upload ")
    })
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseStatus(value = HttpStatus.CREATED)
    public void upload(@RequestPart("file") final MultipartFile file) {
        //action;
    }
9njqaruj

9njqaruj3#

你真的要发送FormData到你的服务器吗?
请尝试将文件包含在FormData对象中,以便HttpClient自动添加多部分边界。

upload(file) {
    const formData = new FormData();
    formData.append('file', file);
    const req = new HttpRequest('POST', this.urlUpload, formData, {
      headers: new HttpHeaders({'Content-Type':'multipart/form-data'}),
      reportProgress: true
    });
    return this.http.request(req);
  }

相关问题