Jasny Bootstrap上传不起作用

时间:2014-10-26 08:28:00

标签: twitter-bootstrap file-upload coldfusion jasny-bootstrap

我正在使用jasny上传,功能非常好,立即进行图像预览,我面临的问题是:文件名在转储中显示为abc.jpg

简单表单提交和jasny上传者的明显区别:

在Coldfusion中,我们上传文件并转储文件,它在末尾显示一些包含tmp file的长字符串,而在jasny的上传功能中,它显示上传的文件名称为abc.jpg,所以我认为这就是问题所在:

因为我的coldfusion上传代码总是说一个错误:

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

虽然Jasny的代码在form内部是这样的,form的内容类型为multipart/form-data

代码:

<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代码:

<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#">

1 个答案:

答案 0 :(得分:0)

尝试更改此行代码:

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

对此:

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

cffile标记只需要表单字段名称myimage,而不是form.myimage