1

我使用Dio框架在我的颤振应用程序中将图像上传到服务器。迪奥版本 3.0.9。发帖方式。添加了 4 个标题创建了带有图像和其他字段的表单数据。

我分析了更多的方法。就像将 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 个异常。任何帮助都会有所帮助

4

1 回答 1

1

我不确定是什么原因造成的,此代码用于我根据您的代码进行更改的应用程序中,但我没有发送任何标题,因此您需要添加然后尝试使用此代码让我知道它对您有用。也确保你有文件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'];
于 2020-09-15T07:51:31.113 回答