Uploads file to a directory on the server.
fileUpload(destination [, fileField] [, mimeType] [, onConflict] [, strict] [, allowedExtensions] [, continueOnError] [, errorVariable])
→ returns struct
coldfusion.tagext.io.FileUtils$InvalidUploadTypeExceptionis thrown..txt,.jpg, not txt, *.txt, or *.*. You can use * as a wildcard to accept all files.
errorerrorskipoverwritemakeuniquetruemimeType parameter.true: The first few bytes of the uploaded file are used to determine the MIME type.false: The MIME type provided by the browser in the request payload is used.
.png,.jpg. Use * to allow all. Overrides the blocked extensions in the server or application settings. Added in CF2018 Update 3, CF2016 Update 10 and CF11 Update 18.
falsetrue, keep uploading the remaining files when one fails, for example because it already exists or has a blocked type or MIME type.
clientFile, reason and message. Use with continueOnError.
<form method="post" enctype="multipart/form-data">
<input type="file" name="fileInput">
<button type="submit">Upload</button>
</form>
<cfscript>
if( structKeyExists( form, "fileInput" )) {
try {
uploadedFile = fileUpload( getTempDirectory(), "fileInput", "image/jpeg,image/pjpeg", "MakeUnique" );
// check the file extension of the uploaded file; mime types can be spoofed
if (not listFindNoCase("jpg,jpeg", uploadedFile.serverFileExt)) {
throw("The uploaded file is not of type JPG.");
}
// do stuff with uploadedFile...
} catch ( coldfusion.tagext.io.FileUtils$InvalidUploadTypeException e ) {
writeOutput( "This upload form only accepts JPEG files." );
}
catch (any e) {
writeOutput( "An error occurred while uploading your file: #e.message#" );
}
}
</cfscript>
Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.