Global Helpers
mimeTypes()
Signature
Section titled “Signature”mimeTypes() — returns string
Available in: controller, model, test, migrator, migration, tabledefinition
Category: Miscellaneous Functions
Description
Section titled “Description”Returns the associated MIME type for a given file extension. Useful when serving files dynamically or setting response headers.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
extension | string | yes | — | The extension to get the MIME type for. |
fallback | string | no | application/octet-stream | The fallback MIME type to return. |
Examples
Section titled “Examples”1. Basic Known Extension
// Get the MIME type for a known extension
mimeType = mimeTypes("jpg");
writeOutput(mimeType); // Outputs: "image/jpeg"
2. Unknown Extension With Fallback
// Use a fallback for unknown file types
mimeType = mimeTypes("abc", fallback="text/plain");
writeOutput(mimeType); // Outputs: "text/plain"
3. Dynamic Extension From User Input
params.type = "pdf";
mimeType = mimeTypes(extension=params.type);
writeOutput(mimeType); // Outputs: "application/pdf"
4. Serving a File Download
fileName = "report.xlsx";
fileExt = listLast(fileName, ".");
cfheader(name="Content-Disposition", value="attachment; filename=#fileName#");
cfcontent(type=mimeTypes(fileExt), file="#expandPath('./public/files/' & fileName)#");