Demo ajaxProxy.cfc for uploading files


A reader noted he was having some problems working with my simple jQuery multi-file uploader.  He asked that I post the ajaxProxy that handles file submissions, so here is the stripped down version:

<cfcomponent output="no"> 
 <cffunction name="Upload" access="remote" returntype="struct" output="yes" returnFormat="JSON" >  
  <cfscript>   
    var base = '/var/www/localhost/';   
    var filePath = base;   
    var thumbPath = base & '/thumbs';   
    var newfile = '';   
    var height = 0;   
    var width = 0;   
    var imgObj = '';   
    var imgStruct = structNew();   
    var response = structNew();   
    var resizeParent = false;   

    response['svrCode'] = '0';   
    response['svrMessage'] = 'Upload successful';  
   </cfscript>  
   <cfsetting requesttimeout="300" />  
   <cftry>   
    <cfif filePath is base>    
     <cfthrow message="Invalid file path" />   
    </cfif>   
    <cfif not DirectoryExists(filePath)>     
      <cfdirectory action="create" directory="#filepath#" />   
    </cfif>   
    <cfif not DirectoryExists(thumbPath)>    
     <cfdirectory action="create" directory="#thumbPath#" />   
    </cfif>   
    <cffile action="upload" filefield="filename" destination="#filePath#" accept="image/jpeg,image/gif,image/x-png,image/pjpeg" nameconflict="MakeUnique" />   
    <cfset newfile="#CreateUUID()#.#cffile.ServerFileExt#" />   
    <cffile action="rename" source="#filePath#/#cffile.serverfile#" destination="#filePath#/#newfile#" />   
    <cfimage action="read" source="#filePath#/#newfile#" name="uploadedFile" />   
    <cfset width = ImageGetWidth(uploadedFile) />   
    <cfset height = ImageGetHeight(uploadedFile) />   
    <cfif (width gt 1024) or (height gt 768)>    
     <cfif width gt height>     
      <cfimage action="resize" width="1024" height="" source="#uploadedFile#" destination="#filePath#/#newfile#" overwrite="true" />    
     <cfelse>     
      <cfimage action="resize" width="" height="768" source="#uploadedFile#" destination="#filePath#/#newfile#" overwrite="true" />    
     </cfif>   
    </cfif>   
   <cfif width gt height>    
    <cfimage action="resize" width="150" height="" source="#uploadedFile#" destination="#thumbPath#/#newfile#" overwrite="true" />   
   <cfelse>    
    <cfimage action="resize" width="" height="150" source="#uploadedFile#" destination="#thumbPath#/#newfile#" overwrite="true" />   
   </cfif>   
   <cfcatch type="any">    
    <cfscript>     
      response['svrCode'] = "-1";     
      response['svrMessage'] = "Error uploading file to the server!";    
    </cfscript>   
   </cfcatch>  
  </cftry>  
  <cfreturn response> 
 </cffunction>
</cfcomponent>

This handles uploading an image and resizing.

Advertisements

About Robert Zehnder

Web application developer specializing in ColdFusion/Railo and Open Source development.

Posted on February 18, 2011, in ColdFusion. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: