Flutter:如何在同一请求中传递Image和Object类?

时间:2019-07-26 18:12:29

标签: flutter

我使用了以下内容: How to pass body @RequestBody and @RequestParam in same request

它与String一起工作,但是如果我使用的是自定义对象类,则会收到415-http响应,其中包含:application / octett-stream ..

uploadDio() async {
    Dto upload = Dto(
        "name",
        "test",
        51123123123,
        52123123123,
        30);

    Response response;
    Dio dio = new Dio();

    FormData formData =
        new FormData.from({"token": "token", "image": new UploadFileInfo(image, "image.png")});

    var jsonEncoded = json.encode(upload.toJson());
    formData.add("dto", jsonEncoded);

    print("dto json: $jsonEncoded");

    response = await dio.post("url", data: formData);

    print(response.statusCode);
  }

那么如何将带有令牌和自定义对象类的文件发布到后端?

这是后端功能:

@PostMapping(value="/uploadDto", consumes = {"multipart/form-data"})
    @ResponseBody
    public StatusResponse createDto(@RequestPart("dto") @Valid @NotNull Dto dto, @RequestPart("image") @Valid @NotNull MultipartFile image)

任何帮助表示赞赏:)

0 个答案:

没有答案