在开发ASP.NET项目时,经常遇到要上传文件的情况,出于安全考虑,我们不但要对上传文件的扩展名进行判断,同时还要对上传文件的大小等进行控制。如果每次遇到上传文件时都写一堆的代码,那不但浪费了时间,同时也会使程序的效率降低。于是我将上传文件写成一个类,这样以后就可以调用类的方法上传文件了。
方法1:判断文件扩展名是否合法
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
IsAllowedExtension#region IsAllowedExtension
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////<summary>
3
///是否允许该扩展名上传
4
///</summary>
5
///<paramname = "hifile">HtmlInputFile控件</param>
6
///<returns>允许则返回true,否则返回false</returns>
7
public static bool IsAllowedExtension(HtmlInputFile hifile)
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
string strOldFilePath = "",strExtension = "";
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
//允许上传的扩展名,可以改成从配置文件中读出
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] arrExtension =
{".gif",".jpg",".jpeg",".bmp",".png"};
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
if(hifile.PostedFile.FileName!= string.Empty)
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
strOldFilePath = hifile.PostedFile.FileName;
17
//取得上传文件的扩展名
18
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
19
//判断该扩展名是否合法
20
for(int i = 0;i<arrExtension.Length;i++)
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
if(strExtension.Equals(arrExtension[i]))
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
return true;
25
}
26
}
27
}
28
return false;
29
}
30
#endregion
31![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
方法2:判断上传文件大小是否超过最大值
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
IsAllowedLength#region IsAllowedLength
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
3
/// 判断上传文件大小是否超过最大值
4
/// </summary>
5
/// <param name="hifile">HtmlInputFile控件</param>
6
/// <returns>超过最大值返回false,否则返回true.</returns>
7
public static bool IsAllowedLength(HtmlInputFile hifile)
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
//允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
10
int i = 20;
11
//如果上传文件的大小超过最大值,返回flase,否则返回true.
12
if(hifile.PostedFile.ContentLength > i * 1024)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
return false;
15
}
16
return true;
17
}
18
#endregion
方法3:获得唯一的文件名
为了保证上传文件不会出现覆盖的情况,我们需要根据当前时间对文件进行重新命名,得到唯一的文件名的方法为:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
GetUniqueString#region GetUniqueString
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
3
/// 获取一个不重复的文件名
4
/// </summary>
5
/// <returns></returns>
6
public static string GetUniqueString()
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
//得到的文件名形如:20050922101010
9
return DateTime.Now.ToString("yyyyMMddhhmmss");
10
}
11
#endregion
方法4:删除文件
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
DeleteFile#region DeleteFile
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
3
/// 删除指定文件
4
/// </summary>
5
/// <param name="strAbsolutePath">文件绝对路径</param>
6
/// <param name="strFileName">文件名</param>
7
public static void DeleteFile(string strAbsolutePath, string strFileName)
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
//判断路径最后有没有符号,没有则自动加上
10
if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
//判断要删除的文件是否存在
13
if(File.Exists(strAbsolutePath + strFileName))
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
//删除文件
16
File.Delete(strAbsolutePath + strFileName);
17
}
18
}
19
else
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
if(File.Exists(strAbsolutePath + @"\" + strFileName))
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
File.Delete(strAbsolutePath + @"\" + strFileName);
24
}
25
}
26
}
27
#endregion
28![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
方法5:上传文件
如果上传文件的扩展名和大小均合法,则将文件上传到服务器上,方法为:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
SaveFile#region SaveFile
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
3
/// 上传文件并返回文件名
4
/// </summary>
5
/// <param name="hifile">HtmlInputFile控件</param>
6
/// <param name="strAbsolutePath">绝对路径.如:Server.MapPath(@"Image/upload")与Server.MapPath(@"Image/upload/")均可,用符号亦可</param>
7
/// <returns>返回的文件名即上传后的文件名</returns>
8
public static string SaveFile(HtmlInputFile hifile,string strAbsolutePath)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
string strOldFilePath = "",strExtension = "",strNewFileName = "";
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
//如果上传文件的文件名不为空
13
if(hifile.PostedFile.FileName != string.Empty)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
strOldFilePath = hifile.PostedFile.FileName;
16
//取得上传文件的扩展名
17
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
18
//文件上传后的命名
19
strNewFileName = GetUniqueString() + strExtension;
20
//如果路径末尾为符号,则直接上传文件
21
if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
hifile.PostedFile.SaveAs(strAbsolutePath + strNewFileName);
24
}
25
else
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
hifile.PostedFile.SaveAs(strAbsolutePath + "\\" + strNewFileName);
28
}
29
}
30
return strNewFileName;
31
}
32
#endregion
该方法将文件上传后,会返回上传文件的新文件名,以备将此新文件名插入到数据库中。
方法6:重新上传文件
重新上传文件时,首先要将原来上传过的文件删除,然后上传新文件,并用新文件名覆盖数据库中的旧文件名,这样才完成了重新上传工作,实现代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
CoverFile#region CoverFile
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
3
/// 重新上传文件,删除原有文件
4
/// </summary>
5
/// <param name="ffFile">HtmlInputFile控件</param>
6
/// <param name="strAbsolutePath">绝对路径.如:Server.MapPath(@"Image/upload")与Server.MapPath(@"Image/upload/")均可,用符号亦可</param>
7
/// <param name="strOldFileName">旧文件名</param>
8
public static void CoverFile(HtmlInputFile ffFile,string strAbsolutePath,string strOldFileName)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
10
//获得新文件名
11
string strNewFileName = GetUniqueString();
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
if(ffFile.PostedFile.FileName != string.Empty)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
15
//旧图片不为空时先删除旧图片
16
if(strOldFileName != string.Empty)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
18
DeleteFile(strAbsolutePath,strOldFileName);
19
}
20
SaveFile(ffFile,strAbsolutePath);
21
}
22
}
23
#endregion
24![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)