download function

Middleware download({
  1. String? filename,
})

Changes the response headers in a way, browsers will handle the http response as a download.

By default the browser will derive a filename from url and content type. Optionally a fixed filename can be specified.

Implementation

Middleware download({String? filename}) =>
    (Handler innerHandler) => (Request request) async {
          final response = await innerHandler(request);
          return response.change(headers: {
            'content-disposition':
                'attachment${filename != null ? '; filename=$filename' : ''}'
          });
        };