uploadFile static method
- @Deprecated('Please use uploadFiles instead of uploadFile')
Future
uploadFile(
- String endPoint,
- String filename, {
- String fileKey = 'image',
- bool isUserAvatar = false,
- bool hasAuth = true,
- Map<String, String>? params,
- required String token,
- bool? defaultResponse,
- bool isTypeJson = true,
- String? changeLocalization,
- String requestType = 'POST',
- String tokenKey = 'Bearer ',
- bool? useDefaultURl,
- bool? showLogs,
- bool? usePreCheckFn,
- int? callTimeoutInSec,
})
Implementation
@Deprecated('Please use uploadFiles instead of uploadFile')
static Future<dynamic> uploadFile(
String endPoint,
String filename, {
String fileKey = 'image',
bool isUserAvatar = false,
bool hasAuth = true,
Map<String, String>? params,
required String token,
bool? defaultResponse,
Map<String, String>? customHeader,
bool isTypeJson = true,
String? changeLocalization,
String requestType = 'POST',
String tokenKey = 'Bearer ',
bool? useDefaultURl,
bool? showLogs,
bool? usePreCheckFn,
int? callTimeoutInSec,
}) async {
Uri url = HttpCalls.getRequestURL(endPoint, useDefaultURl: useDefaultURl);
dynamic response;
try {
await callPreCheckFn(usePreCheckFn);
final Map<String, String> header = {};
if ((localization ?? changeLocalization) != null) {
header['X-localization'] = localization ?? changeLocalization ?? '';
header['Accept-Language'] = localization ?? changeLocalization ?? '';
}
if (isTypeJson) {
header[HttpHeaders.contentTypeHeader] = 'application/json';
}
if (hasAuth) {
header[Static.httpCallTokenKey??HttpHeaders.authorizationHeader] = '${Static.canHttpCallAddBearerAsPreToken?tokenKey:''}$token';
}
if (headerAddOns != null) {
header.addAll(headerAddOns!);
}
showLog((customHeader ?? httpHeader ?? header),
showLog: showLogs, logName: endPoint);
var request = MultipartRequest(
requestType,
url,
);
request.files.add(await MultipartFile.fromPath(fileKey, filename));
request.headers.addAll(customHeader ?? httpHeader ?? header);
if (params != null) {
request.fields.addAll(params);
}
var streamedResponse = await request.send();
var result = await Response.fromStream(streamedResponse);
showLog(result.body.toString(),
enableJsonEncode: false, showLog: showLogs, logName: endPoint);
response =
HttpCalls.getDataObject(result, defaultResponse: defaultResponse);
} catch (e) {
response = errorHandler(e, response, defaultResponse);
}
return response;
}