简体   繁体   中英

Dio - Flutter Image Upload returning Socket Exception

I have tried uploading image using formData which Dio plugin supports.

FormData formData = new FormData.from(
        {"profile_image": UploadFileInfo(image, "profile_image.jpg")});
    var response = await _dio.post(ApiConfiguration.getUploadImageUrl().toString(),data: formData);

But its returning error.

DioError [DioErrorType.DEFAULT]: SocketException: OS Error: Connection reset by peer, errno = 54, address = 3.122.199.93, port = 62181

Any help would be appreciated.

UploadFileInfo and FormData.from deprecated in v3. Documentation needs to be clearer and updated. I wasted a good part of a day with this.

I used dio for post a file path with some other information in this way :

 Dio dio = new Dio();
  FormData formData = new FormData();
  formData.add(
    "apiKey",
    "my_api_key",
  );
  formData.add(
    "file",
    "image_path",
  );
  Response response = await dio.post(
    "https://localhost",
    data: formData,
    onSendProgress: (int sent, int total) {
      // do something
    },
  ).catchError((onError) {
    throw Exception('something');
  });

Faced something similar in Release version of the app, Dio was failing to upload images. This could be due to two reasons, one is mentioned above that you are using a deprecated methods in v3 or You can try adding this permission to the Manifest

Add this permission

<uses-permission android:name="android.permission.INTERNET" />

to your app level Manifest.

android/app/src/main/AndroidManifest.xml

Solution can be found here

https://stackoverflow.com/a/59392036/14641365

Although the other work around to upload an image to the server that I used is something as follows

String url = "YOUR_URL_TO_UPLOAD";
FormData formData = FormData.fromMap({
"QUERY_PARAM_NAME": await MultipartFile.fromFile("IMAGE_PATH", filename:"TESTING.jpg"),
});
var response = await dio.post(url, data: formData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM