【问题标题】:Jasny's Bootstrap Upload not workingJasny 的引导上传不起作用
【发布时间】:2014-12-21 15:56:45
【问题描述】:

我正在使用jasny Upload,该功能非常好,可以立即预览图像,我面临的问题是:文件名在转储中显示为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 的代码在 formform 中是这样的,但其编码类型为 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>

冷熔代码:

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

【问题讨论】:

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


    【解决方案1】:

    尝试更改这行代码:

    <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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 2013-01-27
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      相关资源
      最近更新 更多