ContentDisposition (ContentDisposition v1.0.0) View Source

ContentDisposition helps properly formatting Content-Disposition headers.

Inspired by Ruby's content_disposition gem, this package formats a given disposition and optional filename to an acceptable value for the Content-Disposition header. It takes care of encoding, escaping and adds an ASCII fallback.

Examples

# Without filename
iex> ContentDisposition.format(disposition: :inline)
"inline"

# With a filename, and disposition as a string
iex> ContentDisposition.format(disposition: "inline", filename: "kitten.jpg")
"inline; filename=\"kitten.jpg\"; filename*=UTF-8''kitten.jpg"

# With a UTF-8 filename as attachment
iex> ContentDisposition.format(disposition: :attachment, filename: "kïttéñ.jpg")
"attachment; filename=\"k%3Ftt%3F%3F.jpg\"; filename*=UTF-8''k%C3%AFtt%C3%A9%C3%B1.jpg"

Link to this section Summary

Functions

Formats the given options to a standards-compliant Content-Disposition string.

Link to this section Types

Specs

disposition() :: :inline | :attachment | String.t()

Specs

option() :: {:disposition, disposition()} | {:filename, String.t()}

Link to this section Functions

Specs

format([option()]) :: String.t()

Formats the given options to a standards-compliant Content-Disposition string.

Options

  • :disposition - The disposition type to use.
  • :filename - The name of the file. This parameter is optional and when passed will be encoded to ASCII for the traditional field to support older browsers. Any non-ASCII characters (interpreted as codepoints) will be replaced with ?, and the file name will be used as-is for the UTF-8 encoded filename field.

Examples

iex> ContentDisposition.format(disposition: :inline)
"inline"

iex> ContentDisposition.format(disposition: "inline", filename: "kitten.jpg")
"inline; filename=\"kitten.jpg\"; filename*=UTF-8''kitten.jpg"

iex> ContentDisposition.format(disposition: :attachment, filename: "kïttéñ.jpg")
"attachment; filename=\"k%3Ftt%3F%3F.jpg\"; filename*=UTF-8''k%C3%AFtt%C3%A9%C3%B1.jpg"