flutter - 如何使用Multipart上传图像文件?

标签 flutter dart

我试图将带有Cookie的个人资料图片文件上传到服务器。但是我不知道如何上传。

这是我的代码:

  _submit() async {
    Api.updateUserProfileImage(
      context,
      _image,
      await Provider.of<AccountState>(context, listen: false)
          .storage
          .read(key: "cookie"),
    );
  }
  static void updateUserProfileImage(
      BuildContext context, File image, String cookie) async {}

最佳答案

var request = http.MultipartRequest(
                    "POST",
                    Uri.parse(
                      "YourAPILINK",
                    ),
                  );
                  Map<String, String> headers = {
                    'Content-Type': 'multipart/form-data',
                    'token': token
                  };
                  request.headers['token'] = token;
                  request.headers["Content-Type"]='multipart/form-data';
                  request.fields["name"] = "hardik";

                  request.fields["email"] = "h@gmail.com";
                  request.fields["mobile"] = "00000000";
                  request.fields["address"] = "afa";
                  request.fields["city"] = "fsf";

                  if (image != null) {
                    print(image.path.split(".").last);
                    request.files.add(
                      http.MultipartFile.fromBytes(
                        "avatar",
                        image.readAsBytesSync(),
                        filename: "test.${image.path.split(".").last}",
                        contentType: MediaType(
                            "image", "${image.path.split(".").last}"),
                      ),
                    );
                  }
                  request.fields["reminder_interval"] = "1";

                  request.send().then((onValue) {
                    print(onValue.statusCode);

                    print(onValue.headers);
                    print(onValue.contentLength);
                  });

关于flutter - 如何使用Multipart上传图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59591770/

相关文章:

asynchronous - 如何在 Flutter 的异步任务中启动异步任务?

android - 默认打开文件应用程序 flutter

android-studio - 我们可以在 Flutter/dart 的帮助下创建 windows(pc) 应用程序吗?

dart - flutter父组件如何调用子组件?

Flutter Bloc 从网格中删除项目

dart - 如何在 Dartium 中显示膨胀的聚合物元素?

android - Flutter - InkWell 对 Flexible 内部的 onTap 没有反应

dart - 如何在 flutter 中获取 Assets 的文件路径?

mobile - Flutter listview.separator item 点击

dart - 如何在Dart中通过CustomEvent测试详细信息的类型?