UploadFile上传图片-【控件简单应用】

protected   void  Button1_Click( object  sender, EventArgs e)
        {
            
if  (FileUpload1.HasFile)
            {
                
string  fileContentType  =  FileUpload1.PostedFile.ContentType;
                
if  (fileContentType  ==   " image/bmp "   ||  fileContentType  ==   " image/gif "   ||  fileContentType  ==   " image/pjpeg " )
                {
                    
string  name  =  FileUpload1.PostedFile.FileName;                   //  客户端文件路径

                    FileInfo file 
=   new  FileInfo(name);
                    
string  fileName  =  file.Name;                                     //  文件名称
                     string  webFilePath  =  Server.MapPath( " file/ "   +  fileName);         //  服务器端文件路径


                    
if  ( ! File.Exists(webFilePath))
                    {
                        
try
                        {
                            FileUpload1.SaveAs(webFilePath);                                
//  使用 SaveAs 方法保存文件
                     
                            Label1.Text 
=   " 提示:文件“ "   +  fileName  +   " ”成功上传,文件类型为: "   +  FileUpload1.PostedFile.ContentType  +   " ,文件大小为: "   +  FileUpload1.PostedFile.ContentLength  +   " B " ;
                        }
                        
catch  (Exception ex)
                        {
                            Label1.Text 
=   " 提示:文件上传失败,失败原因: "   +  ex.Message;
                        }
                    }
                    
else
                    {
                        Label1.Text 
=   " 提示:文件已经存在,请重命名后上传 " ;
                    }
                }
                
else
                {
                    Label1.Text 
=   " 提示:文件类型不符 " ;
                }
            }


        }

 

转载于:https://www.cnblogs.com/master-zhu/archive/2010/07/23/1783759.html

你可能感兴趣的:(UploadFile上传图片-【控件简单应用】)