【问题标题】:ColdFusion how to get a message from cfcatchColdFusion 如何从 cfcatch 获取消息
【发布时间】:2020-03-12 20:50:55
【问题描述】:

我正在上传 cffile,并且想要捕获 MIME 类型中的任何错误。我写了这段代码:

 <form 
 enctype= "multipart/form-data" 
 method = "post" 
 name   = "templupload"  
 action = "frag2.cfm">

<cftry>
   <cffile action = "upload"
    destination   = "#session.exploc#"
    fileField     = "form.theupload"
    mode          = '666'
    accept        = 'html'
    strict        = 'true'
    result        = 'ss'
    nameConflict  = "Overwrite">

    <!--- bad mime type files --->
    <cfcatch type = 'any'>   
       <cfif FindNoCase("The MIME type or the Extension of the uploaded file", cfcatch.message)>
       <cfoutput>
          <script>
             document.getElementById('tmpl').innerHTML = "error";
          </script>
       </cfoutput>   
       </cfif>
    </cfcatch>

 <cfthrow type="any" message="got an error" />         
</cftry>

当我尝试上传错误的 MIME 类型时,它不会加载,这很好。表单正在提交,这不好,但我稍后会处理。我现在的问题是我无法在任何地方收到有关错误的消息。我尝试了以下方法:

<cfcatch.message = 'error'; 
<script>alert('error');</script>
<script> document.getElementById('tmpl').innerHTML = "error";</script>
   <!--- this 2nd script does not work regardless of whether the tmpl
         id is on the original page or the target page --->  
<cfoutput> error </cfoutput>
<p> error </p>
<cfthrow type = 'any' message = 'error' />
<cfdump var = "#catch#"  or var = '#catch.message#"

我已经在 cfcatch 标记内外尝试了所有这些,但总是在 cftry 标记内。所有这些方法都在我所做的研究中,但没有一个对我有用。

谁能告诉我我在这里做错了什么?

【问题讨论】:

    标签: coldfusion try-catch cfml


    【解决方案1】:

    你在寻找这样的东西吗?

     <cftry>
    
     <cfcatch>
          <cfset request.error = cfcatch.message>
     <cfcatch>
    
    </cftry>
    

    很久以后

    <cfif request.keyExists('error')>
        <cfoutput>#request.error#</cfoutput>
    </cfif>
    

    【讨论】:

    • 但现在它也不接受 .html 文件。该文件是通过以 html 模式保存 Libre Office 文件创建的。它在浏览器中完美打开。在属性下,它显示为 text/html,这正是上传所要求的。有什么想法吗?
    • cffile 中,accept 属性应该是逗号分隔的mime 类型列表。而不是accept = "html",试试accept = "text/html"
    • +1 用于使用keyExists() 而不是旧版structKeyExists()。来到这里堆栈溢出有助于不断提醒我使用现代技术并打破旧的编码习惯。 :)
    • @AdrianJ.Moreno -- yessss -- 那行得通。你认为我还应该包含 text/htm 吗?
    • @user12031119 -- 非常高兴你给我一个使用 keyExists() 的 +1,但这是 James Mohler 提出的。遗憾的是我在这方面还不够好,还没有任何旧的坏习惯。从好的方面来说,在我了解更多之前,我已经创建了很多不太老的糟糕代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 2020-09-05
    • 2020-07-20
    • 2019-10-28
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多