validating file before you upload it to server

Hi there

Wondering if anyone could assist..

I have a cfc in my model that uploads images to my server from a
form..what I am trying to do is validate that the file isnt over a
certain size and is a jpeg file....at the moment in my model cfc I
have the following code...

<cffile
action="upload"
filefield="imageArtist"
destination="/images/"
nameConflict="makeUnique">

<!---Validation--->
<!---Make Sure the file isnt more than 50K--->
<cfif cffile.FileSize GT 51250>
show error to user
<cfelseif cffile.ClientFileExt NEQ "jpg">
show error to userDo something here
<cfelse>
upload file
</cfif>

What I would like to do is move the validation from the model cfc into
my handler cfc for the upload artist image method...but am unsure of
how to achieve this

At the moment my handler looks like this

  <!---Set Artist Image() Method--->
  <cffunction name="setImage" returntype="void" output="false"
access="public" hint="uploads an image for the artist">
  <cfargument name="EVENT" returntype="any">

      <!---Reference to the request collection--->
    <cfset var rc = EVENT.getCollection()>

    <!---Validation--->
    <!---If image field is empty--->
    <cfif rc.imageArtist is "">
    <cfset getPlugin("messagebox").setMessage("warning","please select
an image to upload")>
    <cfelse>
    <!---Logic to prepare view--->
    <cfset artist = createObject
('component',"myapp.model.mdl_artistService")>
                <!---Run upload artist image method--->
    <cfset rc.setArtistImage = artist.setArtistImage(rc.name_art)>
    </cfif>
    <!---Reroute user back to artist page--->
    <cfset setNextRoute("artist/#rc.name_art#")>

    </cffunction>

Any assistance would be appreciated...thanks

There are lots of solutions to this. One would be to have a “validation” object passed (via DI) to your mdl_artistService.cfc which has methods like addError(), hasErrors() etc. in your controller you can then do something like artist.isValid() which internally calls validation.hasErrors(). Then you’d simply do (if hasErrors returns true) do something like artist.getErrors().

You can achieve this without creating a dedicated validation object, but it is worth the effort as you can reuse it.

Is that the kind of thoughts you’re after?

yes, this is the kind of thing i was looking for thanks,

Will have a go with and come back later if any issues if thats ok

Thanks