【问题标题】:How can i post user's image and other profile data using multipart with dio in flutter如何在颤动中使用带有 dio 的 multipart 发布用户的图像和其他个人资料数据
【发布时间】:2021-03-27 00:20:30
【问题描述】:

我是 Flutter 的新手,想了解如何在 Flutter 中使用 Dio 进行多部分发布请求。场景是,在我的应用程序中,我希望允许用户使用多部分发布请求提交他/她的个人资料详细信息和个人资料图片。我正在尝试下面的代码。请在此代码中提供任何其他改进。

try{
  String filename = _image.path.split("/").last;
  FormData formData = new FormData.fromMap({

    "visitor_photo":  await MultipartFile.fromFile(_image.path,filename: filename, contentType: new MediaType('image','png')),
    "type": "image/png"
  });

  Response response =
      await dio.post(url,data: formData,options: Options(
        headers: {
          "accept": "*/*",
          "Authorization": "Bearer 0rwQmHeADz6g",
          "Content-Type" : "multipart/form-data"
        }
      ));
}catch(e){
  print(e);
}

【问题讨论】:

    标签: android-studio flutter dart flutter-dependencies dio


    【解决方案1】:
    Future<String> uploadImage(File file) async {
        String fileName = file.path.split('/').last;
        FormData formData = FormData.fromMap({
            "file":
                await MultipartFile.fromFile(file.path, filename:fileName),
        });
        response = await dio.post("/info", data: formData);
        return response.data['id'];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2019-10-17
      • 1970-01-01
      • 2020-06-05
      • 2019-09-27
      • 2021-03-09
      相关资源
      最近更新 更多