I was working on a system recently that allowed a user to upload images onto the server. It was restricted to files types of images, more specifically 'jpeg' and 'gif' files.
This is easily done with the 'accept' parameter, as documented in the ColdFusion documentation:
Limits the MIME types to accept. Comma-delimited list. For example, the following code permits JPEG and Microsoft Word file uploads:
accept = "image/jpg, application/msword"
The browser uses the file extension to determine file type.
It is important to note here that it is the browser uses the file extension, so renaming an exe to jpg would fool it entirely.
Issues arise when you don't handle an invalid file upload in a friendly manner. In this case when a user tried to upload an incorrect file type they saw a nasty unformatted error message stating that the request could not be processed as the file was the wrong Mime type.
You cannot tell what the file type is until you attempt to upload it, so wrap your cffile tags in a simple try-catch and handle any errors in the same fashion as you normally would, I.E. by handling the system message and instead displaying a nice, user friendly message that doesn't sound like it was written by robots.
<cffile action="upload" destination="#request.uploadPath#" fileField="form.new_image" accept="image/jpeg, image/gif" nameConflict="overwrite">
<cfset attributes.errors.type = 'error'>
<cfset attributes.errors.message = "The type of file you have tried to upload is not allowed, please select a jpg or gif.">
<cfset request.continue = false>
This article deals with a work around for a problem I had been experiencing with the imageCFC ( http://www.opensourcecf.com/imagecfc/ ) open source project. When resizing an image the file permissions were being lost on the edited file. This meant that the server operating system could not read or serve up the file to the browser.
I've been using imageCFC as I have a variety of coldfusion server versions (7 and 8), and I like the fact that in the resize method you can specify the maximum size for the width and height, and it will resize down to that size, and keep the aspect ratio of width-to-height.
I've been dealing with bulk data recently, CSV files of around 500mb or more. These can seriously stress out your pc. In the process of trying to write data import scripts I found an application that has proven its worth a few times over. This application allows you to split text files at points of your choosing.
File Splitter Pro, from wonderwebware. It doesn't look amazing, and it has a relatively simple interface, but after going through three or four alternatives that for the most part couldn't even open a 500mb csv file this has come through every time. What I really like though is the function that allows you to select N number of lines and move them to another file.
There is a trial version available too:
(P.S I don't get commission or anything.)
A recent piece of functionality required a user to be able to upload an image, and view a thumbnail of that image alongside all the other related details that they were entering (item specific data etc).
After having a look through several forums it seemed that people's opinions were split on whether this could be accomplished using Ajax at all. So I decided to go old school and use an iFrame. I'm not a big fan of frames in any incarnation, but in this case I was willing to make an exception.
<iframe id="uploadFrame" name="uploadFrame" src="action.cfm" height="110" width="90" scrolling="no" frameborder="0"></iframe>
<form name="uploader" action="action.cfm" method="post" enctype="multipart/form-data">
<input type="file" name="image" size="5">
<input type="button" name="action" value="Upload" onclick="changeFrame()"/>
document.getElementById('uploader').target = 'uploadFrame';
The form submits to the 'action.cfm' template. This template displays a placeholder image thumbnail initially, but when the form is submitted it performs the file upload using cffile, then displays the newly uploaded file instead of the placeholder. Ideally at this point I would like to resize the image with cfimage, but my hosting company is still using ColdFusion 7, so I may have to use a third party tag to do the same.
<cfset dest = "webroot/root/tmp/">
<cffile action="upload" filefield="form.image" destination="#dest#" nameconflict="makeunique">
<cfset variables.img = cffile.clientFile>
<cfset variables.img = "holder.gif">
<cfoutput><img src="tmp\#variables.img#" height="100" width="75" border="1"></cfoutput>
It is a shame I had to use an iFrame, and it would be really interesting to see if this is possible in a more web 2.0 scripted way.