FileUpload控件的一个实例

 

 #region fileUpload
    
/// <summary>
    
/// 按规定上传图片
    
/// </summary>
    
/// <param name="fu">FileUpload</param>
    
/// <param name="fs">图片大小 Unit:K 不限制为-1</param>
    
/// <param name="w">图片宽度.不限制为-1</param>
    
/// <param name="h">图片高度.不限制为-1</param>
    
/// <param name="lbl">把文件信息显示到Label标签,不显示设置为null</param>
    
/// <param name="img">把文件信息显示到Image标签,不显示设置为null</param>
    private void ImgFileUpload(FileUpload fu,int fs,int w,int h,Label lbl,Image img)
    {
       
if(fu.HasFile)
       {
           
#region 判断大小
           
           
string strErr = "";//错误信息
            if(fu.PostedFile==null)
            {
                strErr 
+= "对不起,上传文件不能为空!\\n";
            }
            
int filesize = fu.PostedFile.ContentLength;
            
if (filesize < 1)
            {
                strErr 
+= "对不起,上传文件不能为空!\\n";
            }
            
if (fs == -1)
            {
                
///不限制大小
            }
            
else
            {
                
if (filesize > fs*1024///byte
                {
                    strErr 
+= "对不起,文件大小不能大于" + fs + "K!\\n";
                } 
            }
            strErr 
= strErr.Trim();
            
if (strErr != "")
            {
                MessageBox.Show(
this, strErr);
                
return;
            }
           
#endregion
            
#region 获得文件信息
            
string filename;
            
string filetype;
            
string savename;
            
            filetype 
= fu.PostedFile.ContentType.ToString();
            filename 
= fu.FileName;
            savename 
= "T_"+System.DateTime.Now.ToString("yyMMddHHmmss"+ "_" + filename;
            
int w1=0;
            
int h1=0;
            strErr 
= "";
            
#endregion
            
#region 判断格式,宽高
            
switch (filetype)
            {
                
case "image/gif":
                
case "image/bmp":
                
case "image/pjpeg"//图片格式
                    {
                        System.IO.Stream s 
= fu.PostedFile.InputStream;
                        System.Drawing.Image i 
= System.Drawing.Image.FromStream(s);
                        w1 
= i.Width;
                        h1 
= i.Height;
                        
///判断宽度和高度
                        if (w != -1)        //进行宽度限制
                        {
                            
if (w1 > w)
                            {
                                strErr 
+= "对不起,该文件格式超过规定"+w+"px的宽度!\\n";                                    
                            }
                        }

                        
if (h != -1)        //进行宽度限制
                        {
                            
if (h1 > h)
                            {
                                strErr 
+= "对不起,该文件格式超过规定"+h+"px的高度!\\n";
                            }
                        }
                        
break;
                    }
                   
                
//case "application/x-shockwave-flash":
                
//case "application/octet-stream":
                
//    break;
                
//case "video/x-ms-wmv":
                
//case "video/mpeg":
                
//case "video/x-ms-asf":
                
//case "video/avi":
                
//case "audio/mpeg":
                
//case "audio/mid":
                
//case "audio/wav":
                
//case "audio/x-ms-wma":
                
//    break;
                
//case "application/vnd.rn-realmedia-vbr":
                
//    break;
                default:
                    strErr 
+= "对不起,不允许该文件格式上传!\\n";
                    
break;
            }
            
if (strErr != "")
            {
                MessageBox.Show(
this, strErr);
                
return;
            }
            
#endregion 

            
string strInfo = "";

            strInfo 
+= "文件名:" + filename + "<br>";
            strInfo 
+= "文件大小:" + filesize + "<br>";
            strInfo 
+= "文件类型:" + filetype + "<br>";
            strInfo 
+= "文件高度:" + h1 + "<br>";
            strInfo 
+= "文件宽度:" + w1 + "<br>";
            
            
//string imgpath = "UpPic" + "\\" + savename; ///图片路径
            string imgpath = "../UploadImages/" +  savename; ///图片路径
            string fullpath = Server.MapPath("../UploadImages"+ "\\" + savename;
            fu.SaveAs(fullpath);

            
if (lbl != null)
            {
                lbl.Text 
= strInfo;
                lbl.Visible 
= true;
            }
            
if (img != null)
            {
                img.ImageUrl 
= imgpath;
                img.Visible 
= true;
            }
        }
    }
    
#endregion
posted @ 2007-06-14 13:11  roboth  阅读(372)  评论(0)    收藏  举报