asp.net图片上传


代码
protected   void  Button1_Click( object  sender, EventArgs e)
    {
        
string  FilePath  =  Server.MapPath( " ./ " +   " File " ; // 将要存放的路径文件File;
         string  newName;    
        
string  picExt;
        
if  (FileUpload1.HasFile)
        {
// 判断选择了文件

            HttpPostedFile HPF 
=  FileUpload1.PostedFile;
            
//  这种方法用单个文件上传,多个文件要用HttpFileCollection

            
if  (HPF.ContentLength  >   0 )
            {
// 判断文件大小              
                picExt  =  System.IO.Path.GetExtension(HPF.FileName).ToLower(); // 获得文件的拓展     
                newName  =  DateTime.Now.ToString( " yyyyMMddHHmmss " );
                newName 
+=  picExt;
                
if  (picExt == " .jpg " || picExt == " .bmp " || picExt == " .png " || picExt == " .gif " )
                {
                    
//  HPF.SaveAs(FilePath + "\\" + System.IO.Path.GetFileName(HPF.FileName));HPF.FileName这个是获取文件的名字
                    HPF.SaveAs(FilePath  +   " \\ "   +  System.IO.Path.GetFileName(newName));
                }
                
else
                {
                    
this .Label1.Text  =   " 图片格式不支持,请选择jpg|png|gif|bmp格式 " ;
                }
             
            }
                  
        }
        
else
        {
            Label1.Text 
=   " 请选择图片 " ;        
        }
        
    }
 

你可能感兴趣的:(asp.net)