Skip to content

Global Helpers

mimeTypes()

mimeTypes() — returns string

Available in: controller, model, test, migrator, migration, tabledefinition Category: Miscellaneous Functions

Returns the associated MIME type for a given file extension. Useful when serving files dynamically or setting response headers.

NameTypeRequiredDefaultDescription
extensionstringyesThe extension to get the MIME type for.
fallbackstringnoapplication/octet-streamThe fallback MIME type to return.
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)#");