uploadFile method

Future<String> uploadFile(
  1. dynamic file,
  2. String code,
  3. String? dir
)

Implementation

Future<String> uploadFile(file, String code, String? dir) async {
  DateTime x = DateTime.now();
  String code1 = code == "AADHAAR"
      ? "AA"
      : code == "PAN"
      ? "PAN"
      : "$dir";
  String location = dir == null ? "id" : "L";
  String fileName =
      '$location/${code1}_${x.year}${x.month}${x.day}${x.hour}${x.minute}${x.second}${x.millisecond}${x.minute}${x.microsecond}';
  List<int> bytes = await file.readAsBytes();
  String base64File = base64Encode(bytes);
  var postData = {
    "image": base64File,
    "name": fileName,
  };
  try {
    final response = await http.post(Uri.parse(UrlResources.upload),
        body: postData, headers: {"document-access-token": accessToken});
    if (response.statusCode == 200) {
      String value1 = response.body.toString();
      String url = '$fileName.jpg';
      return url;
    } else {
      showErrorToast(
        "Something went wrong,please try ",
      );
      throw Exception();
    }
  } on TimeoutException catch (e) {
    showErrorToast(
      "Connection timeout, please try again",
    );
    throw Exception();
  } on SocketException catch (e) {
    showErrorToast(
      "Please check your network connection",
    );
    throw Exception();
  } on Error catch (e) {
    showErrorToast(
      "Something went wrong",
    );
    throw Exception();
  }
}