狂野都城

一壶浊酒喜相逢,古今多少事, 都付笑谈中。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一个好用的C#上传类!

Posted on 2005-12-21 13:37  狂野都城  阅读(2302)  评论(2编辑  收藏  举报
using System;
using System.Web.UI;
using System.IO;

namespace wapmanage
{
    
/// <summary>
    
/// Upload 的摘要说明。
    
/// </summary>

    public class Upload : System.Web.UI.Page
    
{
        
public Upload(){}

        
public string UploadFile(string filePath,int maxSize,string[] fileType,System.Web.UI.HtmlControls.HtmlInputFile TargetFile)
        
{
            
string Result = "UnDefine";
            
bool typeFlag = false;
            
string FilePath = filePath;
            
int MaxSize = maxSize;
            
string strFileName,strNewName,strFilePath;
            
if (TargetFile.PostedFile.FileName=="")
            
{
                
return "FILE_ERR";
            }

            strFileName 
= TargetFile.PostedFile.FileName;
            TargetFile.Accept 
= "*/*";
            strFilePath 
= FilePath;
            
if (Directory.Exists(strFilePath)==false)
            

                Directory.CreateDirectory(strFilePath);
            }

            FileInfo myInfo
=new FileInfo(strFileName);  
            
string strOldName=myInfo.Name;
            strNewName
=strOldName.Substring(strOldName.Length-3,3); 
            strNewName 
= strNewName.ToLower();
            
if( TargetFile.PostedFile.ContentLength <= MaxSize)
            
{
                
for(int i = 0;i<=fileType.GetUpperBound(0);i++)
                
{
                    
if (strNewName.ToLower() == fileType[i].ToString()){typeFlag = true;break;}
                }

                
if(typeFlag)
                
{  
                    
string strFileNameTemp = GetUploadFileName();
                    
string strFilePathTemp = strFilePath ;
                    strOldName 
= strFileNameTemp + "." +strNewName;     
                    strFilePath
=strFilePath + "\\" + strOldName;    
                    TargetFile.PostedFile.SaveAs(strFilePath);
                    Result 
= strOldName;
                    TargetFile.Dispose();
                }

                
else
                
{
                    
return "TYPE_ERR";
                }

            }

            
else
            
{
                
return "SIZE_ERR";
            }

            
return(Result);
        }


        
public string UploadFile(string filePath,int maxSize,string[] fileType, string filename, System.Web.UI.HtmlControls.HtmlInputFile TargetFile)
        
{
            
string Result = "UnDefine";
            
bool typeFlag = false;
            
string FilePath = filePath;
            
int MaxSize = maxSize;
            
string strFileName,strNewName,strFilePath;
            
if (TargetFile.PostedFile.FileName=="")
            
{
                
return "FILE_ERR";
            }

            strFileName 
= TargetFile.PostedFile.FileName;
            TargetFile.Accept 
= "*/*";
            strFilePath 
= FilePath;
            
if (Directory.Exists(strFilePath)==false)
            

                Directory.CreateDirectory(strFilePath);
            }

            FileInfo myInfo
=new FileInfo(strFileName);  
            
string strOldName=myInfo.Name;
            strNewName
=strOldName.Substring(strOldName.Length-3,3); 
            strNewName 
= strNewName.ToLower();
            
if( TargetFile.PostedFile.ContentLength <= MaxSize)
            
{
                
for(int i = 0;i<=fileType.GetUpperBound(0);i++)
                
{
                    
if (strNewName.ToLower() == fileType[i].ToString()){typeFlag = true;break;}
                }

                
if(typeFlag)
                
{  
                    
string strFileNameTemp = filename;
                    
string strFilePathTemp = strFilePath ;
                    strOldName 
= strFileNameTemp + "." +strNewName;     
                    strFilePath
=strFilePath + "\\" + strOldName;    
                    TargetFile.PostedFile.SaveAs(strFilePath);
                    Result 
= strOldName;
                    TargetFile.Dispose();
                }

                
else
                
{
                    
return "TYPE_ERR";
                }

            }

            
else
            
{
                
return "SIZE_ERR";
            }

            
return(Result);
        }


        
private string GetUploadFileName()
        
{
            
string Result = "";
            DateTime time 
= DateTime.Now;   
            Result 
+= time.Year.ToString() + FormatNum(time.Month.ToString(),2+ FormatNum(time.Day.ToString(),2+ FormatNum(time.Hour.ToString(),2+ FormatNum(time.Minute.ToString(),2+ FormatNum(time.Second.ToString(),2)  + FormatNum(time.Millisecond.ToString(),3) ;
            
return(Result);
        }


        
public string FormatNum(string Num,int Bit)
        
{            
            
int L;
            L 
= Num.Length;
            
for(int i = L ;i < Bit ;i++)
            
{
                Num 
= "0" + Num;
            }

            
return(Num);
        }


    }

}


 调用:

   //创建给类别的图片保存目录
   var path
    path = "C:\temp\";
    if (!Directory.Exists(path))
    {
     Directory.CreateDirectory(path);
    }
    
    if(this.file.PostedFile.FileName != "")
    {
     string[] filetype = new string[]{"jpg","gif","png","JPG","GIF","PNG"};
     filename = new Upload().UploadFile(path , 204800, filetype, file);
    }