我让FormData使用dio包将图像上传到数据库,但是我的编译运行为“ flutter run”,出现了错误。
我看到出现的错误是Http状态错误[500]的问题。
Dio dio = new Dio();
dio.options.headers = {
'Content-type': 'multipart/form-data',
'Accept': 'application/json'
};
FormData formData = new FormData.fromMap({
'name': image_name,
'path': add_path + _uniq[0],
'uuid': _uuid,
'content_type': add_file + image_type,
'file': _image[0],
'record': null
});
Map<String, dynamic> token_send = new Map<String, dynamic>();
token_send = {'token': userGlobal['token']};
response_data = await dio.post(
host + apiVersion + "/formuploads/uploadformattachment",
data: formData,
queryParameters: token_send
);
然后我通过“ flutter run”运行它。然后在终端中出现如下错误:
E/flutter ( 2970): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: DioError [DioErrorType.RESPONSE]: Http status error [500]
E/flutter ( 2970): #0 DioMixin._dispatchRequest (package:dio/src/dio.dart:943:7)
E/flutter ( 2970): <asynchronous suspension>
E/flutter ( 2970): #1 DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous
closure>.<anonymous closure> (package:dio/src/dio.dart:828:37)
E/flutter ( 2970): #2 DioMixin.checkIfNeedEnqueue (package:dio/src/dio.dart:1099:22)
E/flutter ( 2970): #3 DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous
closure> (package:dio/src/dio.dart:825:22)
E/flutter ( 2970): #4 new Future.<anonymous closure> (dart:async/future.dart:176:37)
E/flutter ( 2970): #5 _rootRun (dart:async/zone.dart:1120:38)
E/flutter ( 2970): #6 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 2970): #7 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter ( 2970): #8 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
E/flutter ( 2970): #9 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 2970): #10 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 2970): #11 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:947:23)
E/flutter ( 2970): #12 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:21:15)
E/flutter ( 2970): #13 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
E/flutter ( 2970): #14 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
E/flutter ( 2970): #15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
答案 0 :(得分:0)
尝试使用此方法包装文件
FormData formData = new FormData.fromMap({
'name': image_name,
'path': add_path + _uniq[0],
'uuid': _uuid,
'content_type': add_file + image_type,
'file': await MultipartFile.fromFile('your file path', 'your file name with extention'),
'record': null
});