download method

Future<void> download(
  1. File file, {
  2. String? filename,
})

Sends a download as a response.

Implementation

Future<void> download(File file, {String? filename}) async {
  if (!isOpen) throw closed();

  headers['Content-Disposition'] =
      'attachment; filename="${filename ?? file.path}"';
  contentType = MediaType.parse(lookupMimeType(file.path)!);
  headers['content-length'] = file.lengthSync().toString();

  if (!isBuffered) {
    await file.openRead().cast<List<int>>().pipe(this);
  } else {
    buffer!.add(file.readAsBytesSync());
    await close();
  }
}