Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET中的fileupload控件上应用客户端验证来检查文件名是否包含特殊字符?_C#_Asp.net_Javascript - Fatal编程技术网

C# 如何在ASP.NET中的fileupload控件上应用客户端验证来检查文件名是否包含特殊字符?

C# 如何在ASP.NET中的fileupload控件上应用客户端验证来检查文件名是否包含特殊字符?,c#,asp.net,javascript,C#,Asp.net,Javascript,我在ASP.NET3.5平台上工作。 我使用了文件上传控件和asp按钮来上传文件。 每当我试图上传一个包含特殊字符(file#&%.txt)的文件时,它就会显示出来 撞车,把车开走 -------------------------------------------------------------------------------- Server Error in 'myapplication' Application. ---------------------------------

我在ASP.NET3.5平台上工作。 我使用了文件上传控件和asp按钮来上传文件。 每当我试图上传一个包含特殊字符(file#&%.txt)的文件时,它就会显示出来 撞车,把车开走

--------------------------------------------------------------------------------
Server Error in 'myapplication' Application.
--------------------------------------------------------------------------------

A potentially dangerous Request.Files value was detected from the client 
 (filename="...\New Text &#.txt").
Description: Request Validation has detected a potentially dangerous client input
value, and processing of the request has been aborted. This value may indicate an
attempt to compromise the security of your application, such as a cross-site 
scripting attack. You can disable request validation by setting 
validateRequest=false in the Page directive or in the configuration section. 
However, it is strongly recommended that your application explicitly check all 
inputs in this case. 

Exception Details: System.Web.HttpRequestValidationException: A potentially 
dangerous Request.Files value was detected from the client 
      (filename="...\New Text &#.txt").

Source Error: 

An unhandled exception was generated during the execution of the current web 
request. Information regarding the origin and location of the exception can be
identified using the exception stack trace below.  

--------------------------------------------------------------------------------
如何在客户端使用javascript防止此崩溃?

在类似的回答中,您无法“知道”正在客户端上载的文件名,因为浏览器不允许javascript看到。正如我在这个问题上所说的,您可以使用类似SWFupload的东西在客户端提供更多的控制,如果您愿意,可以检测到这一点


您还可以查看有关如何在服务器端禁用验证的一些想法。

一个非常简单的解决方案是,单击按钮(或其他控件)即可验证文件名,该按钮会触发像这样的上载,如果文件名有问题,请停止上载:

<asp:FileUpload ID="fu1" runat="server" />
<asp:Button ID="btn" runat="server" CausesValidation="true" Text="Click" 
           OnClientClick="return ValidateFileName();" /> 

<script type="text/javascript">
    function ValidateFileName() {
        var fu = document.getElementById("<%= fu1.ClientID %>");
        var f = fu.value + "";
        if ((f.indexOf("#", 0) >= 0) || (f.indexOf("$", 0) >= 0) ||
              (f.indexOf("%", 0) >= 0) || (f.indexOf("^", 0) >= 0)) {
            alert("Filename: [" + f + "] contains invalid char");
            return false;//will stop button click event here
        }

        return true;
    }
</script>

函数ValidateFileName(){
var fu=document.getElementById(“”);
var f=fu.value+“”;
如果((f.indexOf(“#”,0)>=0)|(f.indexOf(“$”,0)>=0)||
(f.indexOf(“%,0)>=0)| |(f.indexOf(“^”,0)>=0)){
警报(“文件名:[“+f+”]包含无效字符”);
返回false;//此处将停止按钮单击事件
}
返回true;
}

ASP.NET页面验证只允许您懒惰,而不必检查输入中可能用于某种攻击的字符。但是,如果您遵循良好的编程实践,例如对显示的内容进行Html.encodeing,并使用SQL查询的参数,那么这种验证就没有那么有用了,我发现它会妨碍您


通过在页面指令中设置
validateRequest=false
,在文件上载页面禁用它。只需确保您正在检查该页面上输入的任何其他值。

那么,如果我们无法知道客户端文件的文件名,我们如何验证允许在java脚本帮助下上载的特定扩展名?请你澄清我的疑问好吗?我亲爱的,你的简单解决方案不起作用。你可以在你的机器上运行代码,然后发送答案。如果(f.indexOf(“A”,0)>=0)@subodh
f.indexOf(“A”,0)
是检查文件名中是否有字母“A”“A”只是一个示例,您可以将其更改为您希望在文件名中查找的任何字符。我已经在我的机器上测试过了:我必须搜索一组字符,比如#$%^我们如何搜索?