我正在使用 jasny Upload,该功能非常好,可以立即预览图像,我面临的问题是:文件名在转储中显示为abc.jpg
简单的表单提交和 jasny 的上传器的明显区别:
在 Coldfusion 中,我们上传一个文件并转储一个文件,它显示了一些tmp file
在末尾包含 a 的长字符串,而在 jasny 的上传功能中,它显示了上传的文件名 say abc.jpg
,所以我认为这就是问题所在:
因为我的coldfusion上传代码总是说一个错误:
Error! Unhandled File Upload Error. form field [form.myimage] is not a file field.
虽然 Jasny 的代码在里面是这样的form
并且form
有 enctypemultipart/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#">