如何在浏览窗口中仅筛选.jpg文件?

时间:2013-04-29 07:29:38

标签: javascript jquery asp.net html

我想过滤只有.jpg文件的浏览窗口。

<asp:FileUpload ID="fileDocument" runat="server"></asp:FileUpload>

以下是图片: - enter image description here

这里我希望它只显示.jpg文件。 我怎么能这样做?

4 个答案:

答案 0 :(得分:0)

请参阅本文,了解客户端和服务器端解决方案:

http://aspalliance.com/1614_Adding_Filter_Action_to_FileUpload_Control_of_ASPNET_20.all

答案 1 :(得分:0)

看看这个:

File Upload with RegularExpressionValidator not working with Firefox only IE

但是我相信您正在使用的库可能无法实现您想要做的事情,您可以尝试使用不同的JavaScript库来执行文件选择。

看看这个: http://www.queness.com/post/11434/7-javascript-ajax-file-upload-plugins

答案 2 :(得分:0)

之前我使用过RegularExpressionValidator。

<asp:RegularExpressionValidator ID="validator_fileupload_extension" runat="server"
     ControlToValidate="FileUpload1"
     ErrorMessage="Images are allowed"
     Text="*" 
     ValidationExpression="^.*\.(jpg|JPG|jpeg|JPEG)$">
</asp:RegularExpressionValidator>

答案 3 :(得分:0)

在aspx页面的head部分添加此脚本。

 <script type ="text/javascript">
    var validFilesTypes=["bmp","gif","png","jpg","jpeg","doc","xls"];
    function ValidateFile()
     {
         var file = document.getElementById("<%=FileUpload1.ClientID%>");
         var path = file.value;
         var ext=path.substring(path.lastIndexOf(".")+1,path.length).toLowerCase();
         var isValidFile = false;
         for (var i=0; i<validFilesTypes.length; i++)
            {
              if (ext==validFilesTypes[i])
                 {
                      break;
                 }
            }
         return isValidFile;
     }
</script>