I want to upload my image to the server using the Dio package. I see on the Dio docs that to upload file using form data is like below:
var formData = FormData.fromMap({
'name': 'wendux',
'age': 25,
'file': await MultipartFile.fromFile('./text.txt',filename: 'upload.txt')
});
response = await dio.post('/info', data: formData);
After that, I have a function to send the image to the API. And this is my function:
Future<CommonResponse> submitDocumentSpaj(SubmitDocumentRequest data) async {
final fileName = data.file.path.split('/').last;
final formData = FormData.fromMap({
'multipartFile':
await MultipartFile.fromFile(data.file.path, filename: fileName),
'path_id': 9,
'subFolder': '${data.subFolder}',
'lus_id': 1,
'username': '${data.username}',
'type': data.loginType,
'filename': '$fileName',
'no_temp': data.noTemp,
'last_index': data.lastFile,
});
print('multipartFile --> ${data.file.path}');
print('path_id --> 9');
print('subFolder --> ${data.subFolder}');
print('lus_id --> 1');
print('username --> ${data.username}');
print('type --> ${data.loginType}');
print('filename --> $fileName');
print('no_temp --> ${data.noTemp}');
print('last_index --> ${data.lastFile}');
final response = await dio.post(
'my_url',
data: formData,
options: Options(
contentType:
'multipart/form-data; boundary=<calculated when request is sent>',
receiveTimeout: 60000,
sendTimeout: 60000,
),
);
return CommonResponse.fromJson(response.data);
}
I check the data is not null, but I get the error like below:
flutter: Dio Response Error --> DioError [DioErrorType.DEFAULT]: Converting object to an encodable object failed: Instance of 'FormData'
flutter: #0 _JsonStringifier.writeObject (dart:convert/json.dart:687:7)
flutter: #1 _JsonStringStringifier.printOn (dart:convert/json.dart:876:17)
flutter: #2 _JsonStringStringifier.stringify (dart:convert/json.dart:861:5)
flutter: #3 JsonEncoder.convert (dart:convert/json.dart:261:30)
flutter: #4 JsonCodec.encode (dart:convert/json.dart:171:45)
flutter: #5 jsonEncode (dart:convert/json.dart:81:10)
flutter: #6 LoggingInterceptors.onResponse (package:core/network/logging_interceptors.dart:36:12)
flutter: #7 DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:dio/src/dio.dart:849:37)
flutter: #8 DioMixin.checkIfNeedEnqueue (package:dio/src/dio.dart:1121:22)
flutter: #9 DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure> (package:dio/src/dio.dart:846:22)
flutter: #10 new Future.<anonymous closure> (dart:async/future.dart:175:37)
flutter: #11 _rootRun (dart:async/zone.dart:1182:47)
flutter: #12 _CustomZone.run (dart:async/zone.dart:1093:19)
flutter: #13 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
flutter: #14 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
flutter: #15 _rootRun (dart:async/zone.dart:1190:13)
flutter: #16 _CustomZone.run (dart:async/zone.dart:1093:19)
flutter: #17 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
flutter: #18 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
flutter: #19 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
flutter: #20 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
flutter: #21 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
I already search for the error in google and StackOverflow, but still not fixed. Can anyone know why I must do to fix my error? Because I believe my code is the same as in the docs in dio, but the difference is I add custom headers like my API and also add custom RTO.
And I already check via Postman with the same data and file as from the Flutter, it's success upload to the server, but from Flutter getting an error.
For loading the image I use image_picker package. This is my code:
TextButton(
onPressed: () async {
Dio dio = Dio();
var response;
var multiPartFile = List<MultipartFile>();
multiPartFile.add(MultipartFile.fromFileSync(_image.path,
filename: "image.jpg"));
var formData = FormData.fromMap({
"files": multiPartFile,
});
response = await dio.post(URI, data: formData);
print('RESPONSE: $response');
},
child: Text("POST"),
),
You may also try sending image as base64 :
final pickedFile = await picker.getImage(source: ImageSource.camera,maxHeight: 480,maxWidth: 640);
List<int> imageBytes = await pickedFile.readAsBytes();
String base64Image = base64Encode(imageBytes);
Dio dio = Dio();
var response;
var formData = FormData.fromMap({
"image": '$base64Image',
});
response = await dio.post(URI, 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.