0

I am working with jasny Upload, the feature is pretty nice, of image preview immediately, the issue i am facing is: the file name is shown in the dump as abc.jpg

Noticeable difference in simple form submission and jasny's uploader:

In Coldfusion, we upload a file and dump a file, it shows some long string containing a tmp file at the end while in jasny's Upload functionality, it shows the Uploaded File name say abc.jpg, so what i think is that is the issue:

because my coldfusion Upload Code always says one error:

Error! Unhandled File Upload Error. form field [form.myimage] is not a file field.

While the Jasny's Code is like this inside the form and form has the enctype of multipart/form-data

Code:

<div class="fileupload fileupload-exists" data-provides="fileupload">
                  <div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
                  <img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" /></div>
                  <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;">
                    <img src="/img/imageupload.png">
                  </div>
                  <div>
                    <span class="btn btn-file"><span class="fileupload-new">Select image</span>
                    <span class="fileupload-exists">Change</span><input type="file" name="myimage" /></span>
                    <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
                  </div>
                </div>

Coldfusion Code:

<cfscript>
    variables.validMimeTypes =  {'image/jpeg': {extension: 'jpg'},'image/png': {extension: 'png'}};  
</cfscript>
<cffunction name="getFileMimeType" returntype="string" output="no">
  <cfargument name="filePath" type="string" required="yes">
  <cfreturn getPageContext().getServletContext().getMimeType(arguments.filePath)>
</cffunction>
<cfset newDirectory = "sitephotos">
<cfif NOT DirectoryExists(ExpandPath('/img/#newDirectory#/'))>
  <cfdirectory action="create" directory="#ExpandPath('/img/#newDirectory#/')#">
</cfif>
<cftry>
  <cffile action="upload" filefield="form.myimage" destination="#GetTempDirectory()#" mode="600" accept="#StructKeyList(variables.validMimeTypes)#" result="uploadResult" nameconflict="overwrite" />
  <!--- get actual mime type --->
  <cfset variables.actualMimeType = getFileMimeType(uploadResult.ServerDirectory & '/' & uploadResult.ServerFile)>
  <!--- redundant check with strict="true", does not hurt to double check Adobe --->
  <cfif NOT StructKeyExists(variables.validMimeTypes, variables.actualMimeType)>
    <cffile action="delete" file="#uploadResult.ServerDirectory#/#uploadResult.ServerFile#"  />
    <cfset result="Error! Invalid file type (checked)" />
  </cfif>
  <!--- generate unique filename for move to destination, DO NOT reuse filename sent by user --->
  <cfset filename = uploadResult.ServerFileName & "." & variables.validMimeTypes[variables.actualMimeType]["extension"]>
  <cfset form.uploadfile = filename>
  <cffile action="move" source="#uploadResult.ServerDirectory#/#uploadResult.ServerFile#" destination="#newDirectory#/#filename#" mode="644" />
  <cfset imgFileRead = ImageRead('#newDirectory#/#filename#')>
  <cfset ImageResize(imgFileRead,'821','460')>
  <cfset ImageWrite(imgFileRead,'#newDirectory#/#filename#',1)>
  <cfset result = "File Upload Successful">
  <cfcatch type="any">
    <!--- file is not written to disk if error is thrown  --->
    <!--- prevent zero length files --->
    <cfif FindNoCase("No data was received in the uploaded", cfcatch.message)>
      <cfset result="Error! Zero length file" />
      <!--- prevent invalid file types --->
      <cfelseif FindNoCase("The MIME type or the Extension of the uploaded file", cfcatch.message)>
      <cfset result="Error! Invalid file type" />
      <!--- prevent empty form field --->
      <cfelseif FindNoCase("did not contain a file.", cfcatch.message)>
      <cfset result="Error! Empty form field" />
      <!---all other errors --->
      <cfelse>
      <cfset result="Error! Unhandled File Upload Error. #cfcatch.Detail# #cfcatch.Message#" />
    </cfif>
  </cfcatch>
</cftry>
<cfsavecontent variable="head">
<cfdump var="#result#">
</cfsavecontent>
<cffile action="write" file="#ExpandPath('/error.html')#" output="#head#">
voyeger
  • 139
  • 2
  • 9

1 Answers1

0

Try changing this line of code:

<cffile action="upload" filefield="form.myimage" 
       destination="#GetTempDirectory()#" 
       mode="600" 
       accept="#StructKeyList(variables.validMimeTypes)#" 
       result="uploadResult" 
       nameconflict="overwrite" />

To this:

<cffile action="upload" filefield="myimage" 
     destination="#GetTempDirectory()#" 
     mode="600" 
     accept="#StructKeyList(variables.validMimeTypes)#" 
     result="uploadResult" 
     nameconflict="overwrite" />

The cffile tag is expecting just the form field name myimage, not form.myimage.

Community
  • 1
  • 1
Scott Jibben
  • 2,229
  • 1
  • 14
  • 22