我正在构建一个上传器,我想设置几个参数,以便客户端无法上传任何类型的文件。
这是处理上传的cfm文件
includes/processFileSubmission.cfm
:
<cfparam name="form.fileUpload" default="">
<cfparam name="form.hiddenFileInput" default="">
<cfset dirName = expandPath("../../data/images/volvelle/")>
<cfif len(trim(form.fileUpload))>
<cfset fileName="#form.actual_name#_#form.email#" />
<cfset fileName=Replace(fileName, " ", "-", "ALL") />
<cfset fileName=LCase(fileName) />
<cfset fileSize = createObject("java","java.io.File").init(form.fileUpload)>
<cfif fileSize.length() LT 52428800>
<cffile
action="upload"
result="upload"
accept="image/*"
fileField="fileUpload"
nameconflict="makeunique"
destination="#dirName#/#fileName#__#hiddenFileInput#"
>
</cfif>
<cfdump var="#upload#">
<cfif #upload.CONTENTTYPE# NEQ 'image'>
<cfoutput>Upload file was not an image</cfoutput> // this does not get reached.
<!--- <cflocation url="error.cfm"> --->
<cfelse>
<cfoutput>Upload file was an image</cfoutput> // this does get reached for an image file
</cfif>
</cfif>
在我的html中,我将使用accept属性仅过滤图像文件,但为了测试这一点,我目前已禁用它(我仍然需要进行后端检查)。
所以 html 是这样的
index.cfm
:
<input class="el-7 button" type="file" name="fileUpload" accept="image/*" />
但现在是这样的:
<input class="el-7 button" type="file" name="fileUpload">
如果我上传图像,我可以从
<cfdump var="#upload#" />
看到此输出
这表明
upload.CONTENTTYPE = image
,这是我在脚本底部尝试在我的条件下测试的内容。我对 cfm 的态度一般,但总是混淆变量插值,但我尝试过 <cfif upload.CONTENTTYPE NEQ 'image'>
,但也不起作用。不确定是否需要报价。
但是,如果我随后上传 pdf,
cffile
会失败,但似乎会传回我的 confirm.cfm
文件,该文件应根据表单输入返回错误或成功消息。
这是错误:
Accept image/*
Detail <br>Only files of type image/* can be uploaded. <br>Verify that you are uploading a file of the appropriate type.
Message The MIME type of the uploaded file application/pdf was not accepted by the server.
这是我的
confirm.cfm
文件的内容
<cfparam name="FORM.fileUpload" default="">
<cfparam name="FORM.name" default="">
<cfparam name="FORM.actual_name" default="">
<cfparam name="FORM.email" default="">
<cfparam name="FORM.comments" default="">
<cfparam name="FORM.auth" default="">
<cfparam name="FORM.gcapthca" default="">
<cfif len(trim(form.fileUpload))>
<cfinclude template="includes/processFileSubmission.cfm">
</cfif>
<cfif len(trim(FORM.auth)) NEQ 0 || len(trim(FORM.name)) NEQ 0>
<cflocation url="thanks.cfm">
</cfif>
<div class="content">
<cfparam name="errors" default="">
<cfif isDefined ("FORM.submit")>
<cfif len(trim(FORM.email)) NEQ 0 AND NOT isValid("email", FORM.email)>
<cfset errors = errors & "<li>Please enter a valid email address</li>">
</cfif>
<!--- clean up variables --->
<cfset FORM.fileUpload="">
<cfset FORM.name="">
<cfset FORM.actual_name="">
<cfset FORM.email="">
<cfset FORM.comments="">
<cfset FORM.gcapthca="">
<cfoutput>
<form method="post" action="#CGI.SCRIPT_NAME#" class="m-t-5">
<input type="hidden" name="submitted" value="true">
<cfif LEN(errors) GT 0><!--- checking for errors --->
Errors
<ul class="errors">
#errors#
</ul>
<a href="javascript:history.back(1)">Go back</a>
<cfelse>
Thank you. Your file has been submitted.<br />
<a href="/">slackwise</a><br />
e: email address
We never disclose email addresses to third parties.
</cfif>
</form>
</cfoutput>
</cfif>
</div>
所有这些代码都有效,它适用于一个简单的反馈表单,我只需要对新的图像上传器进行一些错误处理。
TL/DR
如果用户尝试上传非图像文件,我希望有一个特定的文件重定向。然而,此外,在出现任何其他类型的错误时,拥有一些通用的后备重定向页面也可能很有用。我如何实现这个?
编辑
感谢下面评论中的@mykaf。 cftry/cfcatch 块似乎已经成功了。
所以现在我的代码如下所示:
<cfif len(trim(form.fileUpload))>
<cfset fileName="#form.actual_name#_#form.email#" />
<cfset fileName=Replace(fileName, " ", "-", "ALL") />
<cfset fileName=LCase(fileName) />
<cfset fileSize = createObject("java","java.io.File").init(form.fileUpload)>
<cfif fileSize.length() LT 52428800>
<cftry>
<cffile
action="upload"
result="upload"
accept="image/*"
fileField="fileUpload"
nameconflict="makeunique"
destination="#dirName#/#fileName#__#hiddenFileInput#"
>
<cfcatch>
<cfoutput>
<div class="content">
<ul>
<li><cfoutput>#cfcatch.detail#</cfoutput></li>
<li>You <a href="/submit">can try again</a> if you like.</li>
</ul>
</div>
</cfoutput>
<cfabort>
</cfcatch>
</cftry>
</cfif>
</cfif>
我不介意这个问题是否应该结束,或者我可以把这个写下来,@mykaf 应该获得荣誉。
由于它抛出错误并且未到达您的消息,请使用 try/catch 块来捕获错误并按照您自己的方式处理它。