On the boards, I often get questions regarding file uploads. Usually these questions display a lack of knowledge about how a file is treated, and therefore sent, to the server. I have included here a brief tutorial that I hope will help the new programmer in understanding what is happening to the file as it travels from the user's computer to your server.
First off, the form page.
Try this simple form:
<form method="post" action="getfile.cfm" enctype="multipart/form-data"> <input type="file" name="uploadfile" size="40" /> <input type="submit" name="submit" value="Send the File Off" /> </form> |
When a user fills out the form in their local browser and clicks on the submit button, the file will be sent automatically from the user's computer to your server. In the 'form' tag above, the 'method=post' tells the form to send all information in as a 'post' form (not as part of the url string). The 'action' parameter tells the form which page to load after the form is submitted. The 'enctype=multipart/form-data' tells the form that there is a file attached to the form that needs to be sent to the server (without this, the file will not be received by the server at all).
So, here are the steps so far:
- The user opens your 'uploadfile.html' page in their browser. (You could have called this page anything 'upload.cfm', 'uf.html', etc)
- The user clicks on the 'browse button' in the form and chooses which file on their local computer to send to your server.
- The user clicks on the 'submit' button.
- The file is sent with the form to your server
- Your web server recognizes the 'multipart/form-data' enctype and accepts the file, saving it in a temporary (temp) directory on the web server.
- Your web server retrieves the 'action' page (getfile.cfm, could have been called anything as long as it is a CF page) and processes it.
Try this simple CF page:
<cffile action = "upload" fileField = "UPLOADFILE" destination = "#GetDirectoryFromPath(expandpath('*.*'))#"> |
The misleading part? The action says 'upload'. Most people (myself included) read that to mean 'upload from the users computer'. Which can lead the programmer to believe that the file stays on the user's computer until this command is run. Which can also lead the programmer to believe that they have some minute control over the user's computer. What the 'upload' really means is 'copy from temp directory' or 'upload to a permanent directory'.
So we pick up our server activity steps from where we left off above. (web server retrieves the 'action' page)
- Since the action page is a .cfm page the web server sends the page to the ColdFusion engine.
- The ColdFusion engine sees the cffile upload command and;
- Copies the file from the temp directory to the specified destination directory
- If the copy was successful (no 'permission' errors), then the temp directory file is deleted
- ColdFusion creates a structure variable called 'cffile' to contain all of the information returned by the cffile action
- After ColdFusion engine is done with the file, it is sent back to the webserver to be sent as a 'static' html page to the user's browser.
Something I would like to address, that has come up a few times, is this:
A certain browser (ie) made by the same company that makes a certain popular operating system (windows) sends the user's directory information for the file along with that file to the webserver. You can view it by displaying the 'cffile.CLIENTDIRECTORY' variable, after performing the cffile tag. It is important not to rely on this information. Unless you can guarantee that all browsers in your system will be IE (or at least IE7) then this information will not be available for all browsers. The reason is due to the 'sandbox' rules. These rules are there to protect the user from web sites that might want to do too much and possibly jeopardize the user's system. 'IE' has always broken those rules where it sees fit, but other browsers such as Netscape (now dying off), Firefox, Safari, etc respect the sandbox and don't send that information.
No comments:
Post a Comment