[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Only allow specific file types to be uploaded?

Posted on 2006-07-13
4
Medium Priority
?
249 Views
Last Modified: 2013-12-24
I have a form using an input field to upload an image via CFFILE

the code is like this
<input name="picture" type="file" class="file" id="picture" size="35" value="#url.uploadedfile#" />

How can I use CF to validate that only the requested file types are allowed to be uploaded? I want to limit it to jpg/gif/jpeg ...  

do I do it in the form field, or in the <cffile> function?
what is the syntax?
0
Comment
Question by:MichaelEvangelista
  • 2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Plucka earned 500 total points
ID: 17104878
MichaelEvangelista,

Use the <cffile accept paramater ie

<cffile action="upload" accept="image/jpg, image/gif, image/jpeg"

Regards
Plucka
0
 

Author Comment

by:MichaelEvangelista
ID: 17104945
PERFECT, thanks!

I will accept this answer but I do have  a Part II:
if I try to upload a foreign file type, now I get this error::

//////////////
 The MIME type of the uploaded file "application/octet-stream" was not accepted by the server.

Only files of type "image/jpg, image/gif, image/jpeg" can be uploaded.
Verify that you are uploading a file of the appropriate type.
////////////
Plus the big long eror message, etc.

My upload form already has an error message for the image processing part.
I.e. before adding this code I 'could' upload a foreign file type, but would get a nice formatted error message saying it could not be processed.

Now that I am stopping it from being uploaded in the first place,
I would like to somehow connect to that same error message.

It is called with

<cfcase value="notanimage">
<cfset userMessage = "File unable to be processed.<br>(Could be corrupt or using unknown compression.)<br>Please upload a valid picture file (jpg, gif, png.)" />
<cfset userMessageType = "notice" />
</cfcase>

How can I cause rejection of the filetype by the CFFILE accept parameter
to return to the page with that CFCASE set, rather than throwing the default CF error for the wrong file type?

I will gladly add points to this discussion... or however that works... let me know...


** Is there some way I can "catch" this error and
0
 
LVL 18

Expert Comment

by:Plucka
ID: 17104973
Yes use try/catch block

<cftry>
    <cffile action="upload"

    <cfcatch type="any">
        An error occured
    </cfcatch>
</cftry>
0
 

Author Comment

by:MichaelEvangelista
ID: 17105058
PERFECT !!! Thanks so much!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question