Web.config中配置上传文件大小与判断上传文件的类型等一系列规则的方法

web.config中:
None.gif      <!--  设置文件上传时的系统参数 -->
None.gif    
None.gif  
< httpRuntime  maxRequestLength ="10240"
None.gif    useFullyQualifiedRedirectUrl
="true"
None.gif    executionTimeout
="6000"
None.gif   minFreeThreads
="8"  
None.gif   minLocalRequestFreeThreads
="4"  
None.gif   appRequestQueueLimit
="100"  
None.gif    
/>

返回错误信息的方法:
ContractedBlock.gif ExpandedBlockStart.gif 此方法为初始调用的方法为了判断错误返回错误信息
None.gif        //执行上传操作
ExpandedBlockStart.gifContractedBlock.gif
        /**//// <summary>
InBlock.gif        
/// 执行上传操作
InBlock.gif        
/// getFileName 需要指定来自方法:getUpFileName返回的文件名
InBlock.gif        
/// FileType    "Image" 或 "Media" 或 "Flash"
InBlock.gif        
/// ThisMatch   指定你当前程序所在的目录,哪是不断空一定要在最后加"/",例如"../"
InBlock.gif        
/// a.asf   ->    200609090909.asf
ExpandedBlockEnd.gif        
/// </summary>

None.gif        public string UpfilesInstance(System.Web.UI.HtmlControls.HtmlInputFile UpFileControl,string getFileName,string FileType,string ThisMatch)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
string upfilepath  = string.Empty ;
InBlock.gif            
string Errorstr = string.Empty ;
InBlock.gif
InBlock.gif            getFileName 
= getFileName.Substring(getFileName.LastIndexOf("\\")+1);
InBlock.gif
InBlock.gif                    
InBlock.gif            
switch(CheckFnameEx(getFileName,FileType,UpFileControl.PostedFile.ContentLength/1024 ))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "1":
InBlock.gif                
switch(FileType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{    
InBlock.gif                    
case "Media":
InBlock.gif                        Errorstr 
= "上传的文件格式不正确,只能是WMV、WMA、MP3";
InBlock.gif                        
break;
InBlock.gif                    
case "Image":
InBlock.gif                        Errorstr 
="上传的文件格式不正确,只能是JPG、GIF、JPEG";
InBlock.gif                        
break;
InBlock.gif                    
case "Flash":
InBlock.gif                        Errorstr 
="上传的文件格式不正确,只能是WMV、SWF";
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                }

InBlock.gif                    
break;
InBlock.gif                
case "2":
InBlock.gif                
switch(FileType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{    
InBlock.gif                    
case "Image":
InBlock.gif                        Errorstr 
= "上传的文件不成功";
InBlock.gif                        
break;
InBlock.gif                    
case "Media":
InBlock.gif                        Errorstr 
="上传的文件太大不能超过10M";
InBlock.gif                        
break;
InBlock.gif                    
case "Flash":
InBlock.gif                        Errorstr 
="上传的文件不成功";
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                }

InBlock.gif                    
break;
InBlock.gif                
case "3":
InBlock.gif                    Errorstr 
="上传的文件不成功";
InBlock.gif                    
break;
InBlock.gif                
case "0":
InBlock.gif                    Errorstr 
= null;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
//上传文件
InBlock.gif
            if(Errorstr==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
switch(FileType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{    
InBlock.gif                        
case "Image":
InBlock.gif                            upfilepath 
= ThisMatch +System.Configuration.ConfigurationSettings.AppSettings["upPhotoPath"].ToString();
InBlock.gif                            
break;
InBlock.gif                        
case "Media":
InBlock.gif                            upfilepath 
= ThisMatch +System.Configuration.ConfigurationSettings.AppSettings["upPrdctionPath"].ToString();
InBlock.gif                            
break;
InBlock.gif                        
case "Flash":
InBlock.gif                            upfilepath 
= ThisMatch +System.Configuration.ConfigurationSettings.AppSettings["upPhotoPath"].ToString();
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
InBlock.gif                    UpFileControl.PostedFile.SaveAs(Server.MapPath(upfilepath) 
+ getFileName);
InBlock.gif                    Errorstr 
= "1";//OK
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Errorstr 
="上传的文件出错";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return Errorstr;
ExpandedBlockEnd.gif        }

None.gif    

验证上传的文件:
<add key="maxRequestLength" value="10240" /> 10M
filelength上传的文件长度(是字节)应该 /1024 在与配置文件设置值比较.

ContractedBlock.gif ExpandedBlockStart.gif 验证上传的文件
ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
InBlock.gif        
/// 注册用户登录验证,fname为校验文件名,filetype为"Image" 或者 "Media"
InBlock.gif        
/// return 0-验证通过,1-文件格式不对,2-文件过大,3-未知错误
ExpandedBlockEnd.gif        
/// </summary>

None.gif        public string CheckFnameEx(string fname,string filetype,int filelength)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if(fname=="" && filetype=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "3";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//得到文件的后缀名
InBlock.gif
            string exName=fname.Substring(fname.LastIndexOf(".")+1).ToUpper();
InBlock.gif            
if(exName =="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "3";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string checkfilelen =string.Empty ;
InBlock.gif            
switch(filetype)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "Image":
InBlock.gif                    
if(exName!="JPG" && exName!="GIF" && exName!="JPEG")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return "1";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif                
case "Media":
InBlock.gif                    checkfilelen 
= System.Configuration.ConfigurationSettings.AppSettings["maxRequestLength"].ToString();
InBlock.gif                    
if(exName!="WMA" && exName!="WMV" && exName!="MP3")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return "1";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if(filelength > int.Parse(checkfilelen))//判断文件是否大于10M
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            
return "2";
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif                
case "Flash":
InBlock.gif                    
if(exName!="SWF" && exName!="WMV")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return "1";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return "0";
ExpandedBlockEnd.gif        }

None.gif

转载于:https://www.cnblogs.com/hanguoji/archive/2006/04/18/378510.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值