uploadFile method
这是一个上传文件的 请求
Implementation
Future<Map<String, dynamic>> uploadFile(RequestParams requestParams) async {
Response? response;
Map<String, dynamic> responseMap = HashMap();
if (requestParams.baseUrl.isNotEmpty) {
_dio?.options.baseUrl = requestParams.baseUrl;
}
Map<String, dynamic> map = {};
map["files"] = await MultipartFile.fromFile(requestParams.filePath,
filename: getFileName(requestParams.filePath));
FormData formData = FormData.fromMap(map);
String queryParamsUrl = "";
if (requestParams.params.isNotEmpty) {
bool isFirst = true;
requestParams.params.forEach((key, value) {
if (isFirst) {
isFirst = false;
queryParamsUrl = "?$key=$value";
} else {
queryParamsUrl = "$queryParamsUrl&$key=$value";
}
});
}
try {
response = await _dio?.post(requestParams.queryUrl + queryParamsUrl,
data: formData,
options: Options(
headers: requestParams.headers,
));
if (response?.statusCode == 200) {
if (response?.data is Map) {
responseMap = response?.data;
} else {
responseMap = jsonDecode(response?.data);
}
return responseMap;
} else {
responseMap["statusMessage"] = response?.statusMessage;
return responseMap;
}
} on DioError catch (e) {
NetException netException = NetException.create(e);
return {"errorCode": netException.code, "errorMsg": netException.message};
} catch (e) {
return {"errorCode": -1, "errorMsg": "未知异常"};
}
}