在 Flutter 中使用 dio 上传多部分图像文件的问题

Issue with uploading multi part image file with dio in Flutter

我在我的 flutter 应用程序中使用 Dio 框架将图像上传到服务器。迪奥版本 3.0.9。 Post 方法。 添加了 4 headers 已创建包含图像和其他字段的表单数据。

我分析了很多方法。就像将 Dio 降级到 2.3.1,使用 UploadFileInfo 方法。没有成功。然后用 multipartfileupload。终于这个了

Future<bool> createStoreWithDio() async {
    Map<String, String> headers = {
      "Accept": "application/json",
      "authorization": tokenString,
      "authtype": "admin",
      "Content-Type": "multipart/form-data"
    };
    try {
      FormData formData = new FormData.fromMap({
        "logo": await http.MultipartFile.fromPath("logo", imageFile.path,
            contentType: new MediaType('image', 'png')),
        "name": " Bala ios",
        "description": "_description",
        "website": "www.website.com",
        "password": "Test password",
        "user_name": "Test userInformationName",
        "mobile": "9988776655",
        "email": "test@techit.io",
   
      });

      print(formData.fields);
      Response response = await dio
          .post(
        "API",
        data: formData,
        options: Options(
          headers: headers,
        ),
      )
          .then((value) {
        print(value.toString());
      });
      print(response.toString());
    } catch (error) {
      print(error);
    }
  }

imageFile 是我从相机/图库中捕获的文件。

我收到 500 异常。任何帮助都会有帮助

我不确定是什么原因造成的,这个代码用在一个应用程序中我已经根据你的代码进行了更改,但我没有发送任何 headers 所以你需要添加然后尝试使用这个代码让我知道它适用于 you.also 确保你有文件 imageFile.path 还有你的 api url 是否正确 确保您已导入

`'import  package:http_parser/http_parser.dart';
  import 'package:mime/mime.dart';`

 Dio dio = new Dio();
      final mimeTypeData =
      lookupMimeType(imageFile.path, headerBytes: [0xFF, 0xD8]).split('/');
      FormData formData = FormData.fromMap({
        "name": " Bala ios",
        "description": "_description",
        "website": "www.website.com",
        "password": "Test password",
        "user_name": "Test userInformationName",
        "mobile": "9988776655",
        "email": "test@techit.io",
        "logo": await MultipartFile.fromFile(imageFile.path,
            contentType: MediaType(mimeTypeData[0], mimeTypeData[1])),
      });

      var response = await dio.post(
        Urls.ImageInsert,
        data: formData,
      );

      var message = response.data['message'];