|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
I have a form that allows a user to insert data into a SQL database using coldfusion. I need to allow the user of the form to upload an image from their hard drive to the server.
Is code available so that I can do this? Hope someone can help. Cheers peter |
|
#2
|
|||
|
|||
|
FORM PAGE:
<form method="post" action="uploadaction.cfm" name="uploadForm" enctype="multipart/form-data"> <input name="FileContents" type="file"> <br> <input name="submit" type="submit" value="Upload File"> </form> POST PAGE (replace your destination directory and name conflict settings as necessary): <cffile action = "upload" fileField = "FileContents" destination = "c:\files\upload\" accept = "text/html" nameConflict = "MakeUnique"> |
|
#3
|
|||
|
|||
|
Thanks for all your help.
I understand your code apart from what the conflict settings part. What are conflict settings and how do I set them? Is there also a way to restrict the size of an image that the user can upload (either physical size i.e 1mb or dimensions of the image)? A friend of mine using asp bought a program of the internet to upload an image as it uses streaming of the data I think? Is this necessary or available for uploading images in coldfusion? Cheers Peter |
|
#4
|
|||
|
|||
|
The conflict settings are described fully in the CF documentation.
I don't believe there's any way to limit the file size, as the server knows nothing about the file when the transfer starts. You could probably write code that handled some restrictions AFTER the file is uploaded (by looking at the file size and/or the image size), but it would require coding to do it; such functionality is not provided out of the box. You don't need any special add-ons to upload an image or any other type of file to the CF server. Just the code I listed above, and your decisions on what to do with the file after it is uploaded. |
|
#5
|
|||
|
|||
|
Thanks very much for your help. I have got the code working well.
The only other question I have is how do I do error checking so that i can display my own message when the user trys to upload a file type which isn't included in the accept =. Thanks very much. Cheers Peter |
|
#6
|
|||
|
|||
|
You can have the cffile tag throw an error (I believe that is the default setting). Then use a try/catch block to catch the error and redirect them, display an error message, or whatever else you need to do:
<cftry> <cffile action="UPLOAD" filefield="fileContents" destination="c:\" accept = "image/jpg, application/msword"> <cfcatch type="any"> The upload didn't work or the server could not accept your file type. </cfcatch> </cftry> |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Uploading an image |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|