DownloadFile method

FutureOr DownloadFile()

Implementation

FutureOr DownloadFile() async {
  List<int> bytes = [];
  try{
    await for(var data in req!){
      bytes.addAll(data);
    }
    final boundary = req?.headers.contentType?.parameters['boundary'];

    final transformer = MimeMultipartTransformer(boundary!);
    final stream = Stream.fromIterable([bytes]);

    final parts = await transformer.bind(stream).toList();

    for(var part in parts){
      var content_deposition = part.headers['content-deposition'];
      var fileName = RegExp(r'filename="([^"]*)"')
                    .firstMatch(content_deposition!)
                    ?.group(1);

      var content = await part.toList();

      await File(Directory.current.path+'/img/'+fileName!).writeAsBytes(content[0]);



    }
  } on Exception{
    throw CustomException('File not saved!').showError();
  }

}