怎么样通过.NET上传控件源码?
怎么样通过.NET上传控件源码?
using System;
using System.Web.UI.HtmlControls;
using System.IO;
namespace Web.Custom
{
/*
* ----------------------------------------
* 功能: 通过HtmlInputFile控件上传文件类
* 创建:
* 作者: ∮明天去要饭
* 邮箱: kgidwss@hotmail.com
* ---------------------------------------
*/
/// <summary>
/// HtmlInputFileControl 的摘要说明。
/// </summary>
public class HtmlInputFileControl
{
#region HtmlInputFileControl
public HtmlInputFileControl()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
#region IsAllowedExtension
/// <summary>
/// 是否允许该扩展名上传
/// </summary>
/// <param name="hifile">HtmlInputFile控件</param>
/// <returns>允许则返回true,否则返回false</returns>
public static bool IsAllowedExtension(HtmlInputFile hifile)
{
string strOldFilePath = "",strExtension = "";
//允许上传的扩展名,可以改成从配置文件中读出
string[] arrExtension = {".gif",".jpg",".jpeg",".bmp",".png"};
if(hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//判断该扩展名是否合法
for(int i = 0; i< arrExtension.Length; i++)
{
if(strExtension.Equals(arrExtension[i]))
{
return true;
}
}
}
return false;
}
#endregion
#region IsAllowedLength
/// <summary>
/// 判断上传文件大小是否超过最大值
/// </summary>
/// <param name="hifile">HtmlInputFile控件</param>
/// <returns>超过最大值返回false,否则返回true.</returns>
public static bool IsAllowedLength(HtmlInputFile hifile)
{
//允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
int i = 20;
//如果上传文件的大小超过最大值,返回flase,否则返回true.
if(hifile.PostedFile.ContentLength > i * 1024)
{
return false;
}
return true;
}
#endregion
#region SaveFile
/// <summary>
/// 上传文件并返回文件名
/// </summary>
/// <param name="hifile">HtmlInputFile控件</param>
/// <param name="strAbsolutePath">绝对路径.如:Server.MapPath(@"Image/upload")与Server.MapPath(@"Image/upload/")均可,用/符号亦可</param>
/// <returns>返回的文件名即上传后的文件名</returns>
public static string SaveFile(HtmlInputFile hifile,string strAbsolutePath)
{
string strOldFilePath = "",strExtension = "",strNewFileName = "";
if(hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//文件上传后的命名
strNewFileName = GetUniqueString() + strExtension;
if(strAbsolutePath.LastIndexOf("//") == strAbsolutePath.Length)
{
hifile.PostedFile.SaveAs(strAbsolutePath + strNewFileName);
}
else
{
hifile.PostedFile.SaveAs(strAbsolutePath + "//" + strNewFileName);
}
}
return strNewFileName;
}
#endregion
#region CoverFile
/// <summary>
/// 重新上传文件,删除原有文件
/// </summary>
/// <param name="ffFile">HtmlInputFile控件</param>
/// <param name="strAbsolutePath">绝对路径.如:Server.MapPath(@"Image/upload")与Server.MapPath(@"Image/upload/")均可,用/符号亦可</param>
/// <param name="strOldFileName">旧文件名</param>
public static void CoverFile(HtmlInputFile ffFile,string strAbsolutePath,string strOldFileName)
{
//获得新文件名
string strNewFileName = GetUniqueString();
if(ffFile.PostedFile.FileName != string.Empty)
{
//旧图片不为空时先删除旧图片
if(strOldFileName != string.Empty)
{
DeleteFile(strAbsolutePath,strOldFileName);
}
SaveFile(ffFile,strAbsolutePath);
}
}
#endregion
#region DeleteFile
/// <summary>
/// 删除指定文件
/// </summary>
/// <param name="strAbsolutePath">文件绝对路径</param>
/// <param name="strFileName">文件名</param>
public static void DeleteFile(string strAbsolutePath, string strFileName)
{
if(strAbsolutePath.LastIndexOf("//") == strAbsolutePath.Length)
{
if(File.Exists(strAbsolutePath + strFileName))
{
File.Delete(strAbsolutePath + strFileName);
}
}
else
{
if(File.Exists(strAbsolutePath + "//" + strFileName))
{
File.Delete(strAbsolutePath + "//" + strFileName);
}
}
}
#endregion
#region GetUniqueString
/// <summary>
/// 获取一个不重复的文件名
/// </summary>
/// <returns></returns>
public static string GetUniqueString() <font |