zoukankan      html  css  js  c++  java
  • ASP.NET2.0 FileUpload控件添加过滤功能

    第一步:添加控件
    <asp:Label ID="lblUploadErr" runat="server"></asp:Label>
    <asp:FileUpload ID="fileDocument" runat="server" />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" 
                OnClientClick
    ="return CheckForTestFile();" onclick="btnUpload_Click" />

    第二步:添加js代码
    <script type="text/javascript">
            
    ////Trim the input text
            function Trim(input)
            
    {
                
    var lre = /^\s*/
                
    var rre = /\s*$/
                input 
    = input.replace(lre, ""); 
                input 
    = input.replace(rre, ""); 
                
    return input; 
            }

            
            
    // filter the files before Uploading for text file only  
           function CheckForTestFile() 
           
    {
                
    var file = document.getElementById('<%=fileDocument.ClientID%>');
                
    var fileName=file.value;        
                
    //Checking for file browsed or not 
                if (Trim(fileName) =='' )
                
    {
                    alert(
    "Please select a file to upload!!!");
                    file.focus();
                    
    return false;
                }

         
               
    //Setting the extension array for diff. type of text files 
               var extArray = new Array(".txt"".doc"".rtf"".pdf"".sxw"".odt"
                                        
    ".stw"".html"".htm"".sdw"".vor");       
         
               
    //getting the file name
               while (fileName.indexOf("\\"!= -1)
                 fileName 
    = fileName.slice(fileName.indexOf("\\"+ 1);
         
               
    //Getting the file extension                     
               var ext = fileName.slice(fileName.indexOf(".")).toLowerCase();
         
               
    //matching extension with our given extensions.
               for (var i = 0; i < extArray.length; i++
               
    {
                 
    if (extArray[i] == ext) 
                 

                   
    return true;
                 }

               }
      
                 alert(
    "Please only upload files that end in types:  " 
                   
    + (extArray.join("  ")) + "\nPlease select a new "
                   
    + "file to upload and submit again.");
                   file.focus();
                   
    return false;                
           }

    </script>
    第三步:添加服务器端事件
    protected void btnUpload_Click(object sender, EventArgs e)
            
    {
                
    try
                
    {

                    
    //获取上载文件文件大小
                    int intDocFileLength = fileDocument.PostedFile.ContentLength;

                    
    //获取客户端上的文件的完全限定名称
                    string strPostedFileName = fileDocument.PostedFile.FileName;

                    
    //如果大于4MB
                    if (intDocFileLength > 4096000)
                    
    {
                        lblUploadErr.Text 
    = "上传文件大小大于4MB";
                        
    return;
                    }


                    
    if (strPostedFileName != String.Empty)
                    
    {
                        
    //返回指定路径的扩展名,并转成小写
                        string strExtn = System.IO.Path.GetExtension(strPostedFileName).ToLower();

                        
    if ((strExtn == ".txt"|| (strExtn == ".doc"|| (strExtn == ".rtf"|| (strExtn == ".pdf"||
                            (strExtn 
    == ".sxw"|| (strExtn == ".odt"|| (strExtn == ".html"|| (strExtn == ".stw"||
                            (strExtn 
    == ".htm"|| (strExtn == ".sdw"|| (strExtn == ".vor"))
                        
    {
                            
    //返回与服务器上的指定虚拟路径相对应的物理文件路径
                            string savePath = Server.MapPath(ConfigurationManager.AppSettings["UploadedPath"]) + "\\";

                            
    //保存上传的文件内容
                            fileDocument.PostedFile.SaveAs(savePath + System.IO.Path.GetFileName(strPostedFileName));

                            lblUploadErr.Text 
    = "document uploaded successfully";
                        }

                        
    else
                        
    {

                            lblUploadErr.Text 
    = "Please upload a valid Document with the extenion in:<br>";
                            lblUploadErr.Text 
    = ".txt,.doc,.rtf,.pdf,.sxw,.odt,.stw,.html,.htm,.sdw Or .vor";
                        }

                    }

                    
    else
                    
    {

                        lblUploadErr.Text 
    = "There is no file to Upload";
                    }

                }

                
    catch (System.Exception ex)
                
    {
                    lblUploadErr.Text 
    = ex.Message;
                }

            }

    第四步:Web.config
      <appSettings>
        <add key="UploadedPath" value="/"/>
      </appSettings>

    Value值是上传到的虚拟路径,这里写了根目录
  • 相关阅读:
    浅谈UML学习笔记之用例图
    流媒体服务器
    浅谈UML的概念和模型之UML视图
    浅谈UML的概念和模型之UML九种图
    SQL 语法
    吴裕雄--天生自然数据结构:链式队列及基本操作
    吴裕雄--天生自然数据结构:顺序队列
    吴裕雄--天生自然数据结构:队列存储结构
    吴裕雄--天生自然数据结构:链栈及基本操作
    吴裕雄--天生自然数据结构:顺序栈及基本操作
  • 原文地址:https://www.cnblogs.com/yongheng178/p/1271100.html
Copyright © 2011-2022 走看看