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值是上传到的虚拟路径,这里写了根目录

转载于:https://www.cnblogs.com/yongheng178/archive/2008/08/19/1271100.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值