upLoadFile static method
Implementation
static Future<FastHttpResponse> upLoadFile({
required String url,
required File file,
Map<String, dynamic> headers = const {},
Map<String, dynamic>? data, //可选
String? hud,
bool isMultiFile = true,
}) async {
String path = file.path;
var name = path.substring(path.lastIndexOf("/") + 1, path.length);
Map<String, dynamic> body = Map<String, dynamic>();
if (data != null) {
body.addAll(data);
}
if (isMultiFile) {
body.addAll({
"file": [adio.MultipartFile.fromFileSync(path, filename: name)],
});
} else {
body.addAll({
"file": adio.MultipartFile.fromFileSync(path, filename: name),
});
}
adio.FormData formData = adio.FormData.fromMap(body);
var result;
if (hud != null && hud.isNotEmpty) {
EasyLoading.show(status: hud);
}
adio.BaseOptions options = adio.BaseOptions(
connectTimeout: CONNECT_TIMEOUT,
receiveTimeout: RECEIVE_TIMEOUT,
headers: headers,
contentType: headers.isNotEmpty ? headers['Content-Type'] : null,
);
// options.headers = fastDio?.options.headers;
adio.Dio dio = fastDio ?? adio.Dio(options);
var response = await dio
.post("$url", data: formData)
.whenComplete(() => EasyLoading.dismiss());
if (response.statusCode == HttpStatus.ok) {
var data = response.data;
if (data is String) {
result = jsonDecode(data);
} else if (data is Map) {
result = response.data;
}
} else {
result = {"msg": "error", "code": response.statusCode, "msgCode": "1"};
}
return FastHttpResponse(
jsonEncode(result), response.statusCode, response.headers.map, result);
}