download method

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

Sends a download as a response.

The client sees filename, or else the file's base name; never its path on the server. A missing file is a 404. HEAD requests get the headers only.

Implementation

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

  headers['content-disposition'] = attachmentDisposition(
    filename ?? file.basename,
  );
  contentType = _fileContentType(file);
  contentLength = await file.length();

  if (correspondingRequest?.method != 'HEAD') {
    if (!isBuffered) {
      await addStream(file.openRead());
    } else {
      buffer!.add(await file.readAsBytes());
    }
  }
  await close();
}