您的位置:首页 > 编程语言 > C#

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

2009-03-05 17:18 489 查看
类代码~

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单位是字节
}

可以对文件强制命名,也可以自动命名。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐