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单位是字节
}
可以对文件强制命名,也可以自动命名。
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单位是字节
}
可以对文件强制命名,也可以自动命名。
相关文章推荐
- C#上传类,可自主添加文件允许类型,可强制命名
- FCKeditor 上传修改,添加对文件的类型以及大小的限制(ASP.NET C#)
- WordPress 2添加自定义文件上传类型(允许上传rar文件方法)
- FCKeditor 上传修改,添加对文件的类型以及大小的限制(ASP.NET C#)
- c#实现按日期命名上传文件代码
- input只允许上传图片类型文件
- asp.net(c#)上传文件时检测文件类型方法小结
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器
- 当网站不允许上传ASP,CGI,CER,CDX,HTR等类型的文件时
- Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- 当网站不允许上传ASP,CGI,CER,CDX,HTR等类型的文件时
- asp.net(c#)上传文件时检测文件类型方法小结
- C#检测上传文件类型(真实检测,不通过后缀名)
- C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
- C#命名空间的使用、添加命名空间、引用类型和值类型、字符串与char数组转换
- Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据