现在的位置: 首页 > 综合 > 正文

C#上传类,可自主添加文件允许类型,可强制命名

2012年03月05日 ⁄ 综合 ⁄ 共 4738字 ⁄ 字号 评论关闭
类代码~

public class UpLoadFiles : System.Web.UI.Page
{
    
public UpLoadFiles()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }


    
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 ;
                    
float strFileSize = TargetFile.PostedFile.ContentLength / 1024;
                    strOldName 
= strFileNameTemp + "." +strNewName;     
                    strFilePath
=strFilePath + "\\" + strOldName;    
                    TargetFile.PostedFile.SaveAs(strFilePath);
                    Result 
= strOldName + "|" + strFileSize;
                    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);
    }

        
}

调用代码~

string path="";
path 
= Server.MapPath("路径");
if (!Directory.Exists(path)) 
{
    Directory.CreateDirectory(path);
}

if(this.file.PostedFile.FileName != "")
{
    
string[] filetype = new string[]{"jpg","gif"};
    
string filename = new UpLoadFiles().UploadFile(path, 1000000, filetype, file); //1000000单位是字节
}

可以对文件强制命名,也可以自动命名。

抱歉!评论已关闭.