Solved

Only allow specific file types to be uploaded?

Posted on 2006-07-13
4
231 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 125 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now