【问题标题】:How to handle cffile specific errors in ColdFusion如何在 ColdFusion 中处理 cffile 特定错误
【发布时间】:2011-02-22 00:46:23
【问题描述】:

我正在使用带有 accept 属性的 cffile 标记。如果用户上传的 mime 类型不是我允许的,我想阻止它,但同时我希望能够处理错误。我尝试使用 try catch 块,但仍然出错。

<cftry>
  <cffile action="upload"
          filefield = "txtImg"
          accept="image/jpeg, image/png, image/bmp, image/gif, image/pjpeg, image/x-png"
          destination="E:\www2\website\Home\uploads\Images\"
          nameconflict="makeunique">
    <cfcatch>
        <script>
            $(function(){ 
                alert("Only the following file types are allowed: .jpg, .gif, .bmp, .png.");
            });
        </script>
    </cfcatch>
  </cftry>  

【问题讨论】:

  • 你说它仍然错误,但你收到的错误文本是什么?

标签: coldfusion


【解决方案1】:

您必须分析cfcatch 消息。稍加修改的代码示例版本可能如下所示:

<cftry>

    <cffile action="upload"
        filefield = "txtImg"
        accept="image/jpeg, image/png, image/bmp, image/gif, image/pjpeg, image/x-png"
        destination="/tmp/"
        nameconflict="makeunique"/ >

    <cfcatch>

        <cfif FindNoCase("not accepted", cfcatch.Message)>
            <script>
                $(function(){
                    alert("Only the following file types are allowed: .jpg, .gif, .bmp, .png.");
                });
            </script>
            <cfabort />
        <cfelse>
            <!--- looks like non-MIME error, handle separately --->
            <cfdump var="#cfcatch#" abort />
        </cfif>

    </cfcatch>

</cftry>

请注意,对于所有 CF 版本,我不确定是否会因不正确的 mime 类型而引发的确切消息,因此我使用了来自 ACF9 的按消息块搜索,如下所示:The MIME type of the uploaded file application/octet-stream was not accepted by the server.

【讨论】:

    【解决方案2】:

    你也可以使用:

    <cfcatch>
        <cfif IsDefined("cfcatch.MimeType")>
            <!--- Do stuff --->
        </cfif>
    </cfcatch>
    

    通常在定义&lt;cffile&gt; 组件的accept 属性时,它会引发MimeType 类型的自定义错误。

    或者您可以只使用&lt;cfcatch&gt; 处理MimeType 错误。

    <cfcatch type="MimeType">
        <!--- Do stuff --->
    </cfcatch>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多