zoukankan      html  css  js  c++  java
  • Web.config中配置上传文件大小与判断上传文件的类型等一系列规则的方法

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

    返回错误信息的方法:

            //执行上传操作
            /// <summary>
            
    /// 执行上传操作
            
    /// getFileName 需要指定来自方法:getUpFileName返回的文件名
            
    /// FileType    "Image" 或 "Media" 或 "Flash"
            
    /// ThisMatch   指定你当前程序所在的目录,哪是不断空一定要在最后加"/",例如"../"
            
    /// a.asf   ->    200609090909.asf
            
    /// </summary>

            public string UpfilesInstance(System.Web.UI.HtmlControls.HtmlInputFile UpFileControl,string getFileName,string FileType,string ThisMatch)
            
    {
                
    string upfilepath  = string.Empty ;
                
    string Errorstr = string.Empty ;

                getFileName 
    = getFileName.Substring(getFileName.LastIndexOf("\\")+1);

                        
                
    switch(CheckFnameEx(getFileName,FileType,UpFileControl.PostedFile.ContentLength/1024 ))
                
    {
                    
    case "1":
                    
    switch(FileType)
                    
    {    
                        
    case "Media":
                            Errorstr 
    = "上传的文件格式不正确,只能是WMV、WMA、MP3";
                            
    break;
                        
    case "Image":
                            Errorstr 
    ="上传的文件格式不正确,只能是JPG、GIF、JPEG";
                            
    break;
                        
    case "Flash":
                            Errorstr 
    ="上传的文件格式不正确,只能是WMV、SWF";
                            
    break;
                    }

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

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

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

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

                    
    catch
                    
    {
                        Errorstr 
    ="上传的文件出错";
                    }

                }


                
    return Errorstr;
            }

        

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

    验证上传的文件
  • 相关阅读:
    APP兼容性测试
    APP本地服务安全测试
    接口安全测试
    Python之日志操作(logging)
    Python之json编码
    Python之配置文件读写
    windows10 修改远程连接本地端口
    ctf学习
    telnet常见的错误
    连接ssh中常见的错误代码
  • 原文地址:https://www.cnblogs.com/hanguoji/p/378510.html
Copyright © 2011-2022 走看看