ContentDisposition class final

The value of a Content-Disposition header: whether a body is shown by the browser (inline), saved as a file (attachment) or is one field of a multipart form (form-data), with the file or field name.

A controller sets it on a download so that the browser saves the file under a chosen name; each part of a multipart body has a form-data one. A non-ASCII file name is written as filename* (RFC 5987, UTF-8) and read back from it, so any name is safe.

// A download the browser saves as report.pdf instead of showing it.
@GetMapping('/reports/{id}.pdf')
ResponseEntity<Uint8List> report(@PathVariable() String id) {
  final headers = HttpHeaders()
    ..contentType = 'application/pdf'
    ..contentDisposition =
        const ContentDisposition.attachment(filename: 'report.pdf');
  return ResponseEntity(200, body: reports.pdf(id), headers: headers);
}

Spring (Java):

headers.setContentDisposition(
    ContentDisposition.attachment().filename("report.pdf").build());
headers.getContentDisposition().getFilename();

Constructors

ContentDisposition(String type, {String? name, String? filename})
A disposition of any type, with an optional name and filename; the named constructors cover the three usual types.
const
ContentDisposition.attachment({String? filename})
attachment: the browser saves the body as a file, named filename when given.
const
ContentDisposition.formData({required String name, String? filename})
form-data: the part of a multipart form for the field name; filename makes it a file.
const
ContentDisposition.inline({String? filename})
inline: the browser shows the body, as it does without the header; filename is the name offered if the user saves it.
const

Properties

filename → String?
The file name, from filename* (decoded) or filename; null when there is none.
final
hashCode → int
The hash code for this object.
no setteroverride
isAttachment → bool
Whether the type is attachment.
no setter
isFormData → bool
Whether the type is form-data.
no setter
isInline → bool
Whether the type is inline.
no setter
name → String?
The name parameter, the form field of a form-data part; null otherwise.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
type → String
The disposition type: attachment, inline, form-data, or another; lower case when parsed.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
The header value, such as attachment; filename="report.pdf". Quotes and backslashes in the names are escaped; a file name that is not printable ASCII is written as filename*=UTF-8''....
override

Operators

operator ==(Object other) → bool
The equality operator.
override

Static Methods

parse(String value) → ContentDisposition
Reads a header value such as attachment; filename="report.pdf", like Spring's ContentDisposition.parse. The type is lower-cased, quotes and backslash escapes are removed, filename* wins over filename, and other parameters are ignored. Throws an ArgumentError when the value has no type.