downloadFile method

  1. @override
Future<String?> downloadFile(
  1. String url,
  2. Map<String, dynamic> body, {
  3. String fallbackFilename = "file",
})
override

Implementation

@override
Future<String?> downloadFile(String url, Map<String, dynamic> body, {String fallbackFilename = "file"}) async {
  if (Platform.isAndroid && !(await hasWriteExternalStoragePermission)) {
    requestAndroidPermission();
    return null;
  }
  http.Response response = await http.post(
    Uri.parse(url),
    body: body,
  );

  Directory dir = await dirPath();
  FileInfo fileInfo = parseFileInfo(response.headers["content-disposition"]) ?? FileInfo(fallbackFilename, "");
  int index = 0;
  String filePath = "${dir.path}${Platform.pathSeparator}${fileInfo.fileName}${fileInfo.fileExtension}";
  while (await File(filePath).exists()) {
    filePath = "${dir.path}${Platform.pathSeparator}${fileInfo.fileName}(${++index})${fileInfo.fileExtension}";
  }
  await File(filePath).writeAsBytes(response.bodyBytes);
  return filePath;
}