调用可以很灵活,代码如下:
using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Text;
using System.IO;
namespace aspnet_test
{
/// <summary>
/// 单个文件上传类
///
/// 可以自定义:上传目录,上传文件类型、大小,是否使用随机上传文件名。
///
/// 作者:laifangsong QQ:25313644 GMail:laifangsong@gmail.com
/// 原文:http://www.cnblogs.com/jiny-z/archive/2007/05/30/764762.html
/// </summary>
public class UploadFile_Single
{
/* web.config设置:
* <appSettings>
* <add key="uploadDir" value="UploadFiles/" />
* <add key="uploadFileExt" value="gif|jpg|jpeg|png|bmp|doc|xls|hlp|chm|rar|zip" />
* <add key="uploadFileSize" value="2048" />
* <add key="isUseRandFileName" value="true" />
* </appSettings>
*
* private string uploadDir = ConfigurationSettings.AppSettings["uploadDir"];;
* private string uploadFileExt = ConfigurationSettings.AppSettings["uploadFileExt"];
* private int uploadFileSize = int.Parse(ConfigurationSettings.AppSettings["uploadFileExt"]);
* private bool isUseRandFileName = bool.Parse(ConfigurationSettings.AppSettings["uploadFileExt"]);
* */
//上传目录,相对于本文件路径;以“/”结尾;如果目录不存在,会自动创建;如果要启用绝对路径,写法:“/虚拟目录名称/上传文件目录名称/”
private string uploadDir = "UploadFiles/";
//允许上传文件类型,逗号隔开
private string uploadFileExt = "gif|jpg|jpeg|png|bmp|doc|xls|hlp|chm|rar|zip";
//允许上传文件大小,k 为单位
private int uploadFileSize = 2048;
//是否使用随机上传文件名;如果不使用,会覆盖上传目录的同名文件
private bool isUseRandFileName = true;
//上传结果
private string uploadResult = "";
//上传文件保存路径
private string uploadPath = "";
public string UploadDir
{
get
{
return uploadDir;
}
set
{
uploadDir = value;
}
}
public string UploadFileExt
{
get
{
return uploadFileExt;
}
set
{
uploadFileExt = value;
}
}
public int UploadFileSize
{
get
{
return uploadFileSize;
}
set
{
uploadFileSize = value;
}
}
public bool IsUseRandFileName
{
get
{
return isUseRandFileName;
}
set
{
isUseRandFileName = value;
}
}
public string UploadResult
{
get
{
return uploadResult;
}
}
public string UploadPath
{
get
{
return uploadPath;
}
}
//上传文件方法
public bool Upload(Page page)
{
HttpPostedFile uploadFile = page.Request.Files[0];
//获取上传文件属性
string fileFullName = uploadFile.FileName;
string contentType = uploadFile.ContentType;
int contentLength = uploadFile.ContentLength;
//取得文件名
string fileName = System.IO.Path.GetFileName(fileFullName);
//取得扩展名
string fileExt = System.IO.Path.GetExtension(fileName).Remove(0, 1);
//检测文件类型
if( ("|"+UploadFileExt+"|").IndexOf( ("|"+fileExt+"|") ) < 0 )
{
uploadResult = "<font color='red'><b>上传文件格式错误!允许上传文件类型(" + uploadFileExt + ")</b></font>";
return false;
}
//检测文件大小
if(contentLength<1 || contentLength>uploadFileSize*1024)
{
uploadResult = "<font color='red'><b>上传文件大小错误!最大允许上传 " + uploadFileSize + " KB</b></font>";
return false;
}
//如果上传目录不存在,创建目录
if(!uploadDir.EndsWith("/")) uploadDir += "/";
string tempPath = "";
string tempMapPath = "";
string tempDir = uploadDir.Substring(0,1)!="/" ? uploadDir : uploadDir.Substring(uploadDir.IndexOf("/", 2)+1) ;
while(tempDir.IndexOf("/")>=0)
{
tempPath += tempDir.Substring(0, tempDir.IndexOf("/")+1);
tempMapPath = System.Web.HttpContext.Current.Server.MapPath(tempPath);
if(!Directory.Exists(tempMapPath))
{
Directory.CreateDirectory(tempMapPath);
}
tempDir = tempDir.Substring(tempDir.IndexOf("/")+1);
}
//随机文件名
if(isUseRandFileName)
{
fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + (new Random()).Next(100, 1000).ToString() + "." + fileExt;
}
string saveFilePath = System.Web.HttpContext.Current.Server.MapPath(uploadDir) + @"\" + fileName;
uploadFile.SaveAs(saveFilePath);
uploadPath = uploadDir+fileName;
//SaveUploadPathToDB(uploadPath);
uploadResult += "<font color='blue'><b>文件上传成功!</b></font>" + "<br/><br/>" +
"<b>保存位置:</b>" + uploadPath;
return true;
}
//将上传文件路径存入数据库
private void SaveUploadPathToDB(string uploadPath)
{
//string sql = "update table 1 set f_uploadpath='" & uploadPath & "' where ";
//TSQL.ExecuteNonQuery(sql, null);
}
}
}
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Text;
using System.IO;
namespace aspnet_test
{
/// <summary>
/// 单个文件上传类
///
/// 可以自定义:上传目录,上传文件类型、大小,是否使用随机上传文件名。
///
/// 作者:laifangsong QQ:25313644 GMail:laifangsong@gmail.com
/// 原文:http://www.cnblogs.com/jiny-z/archive/2007/05/30/764762.html
/// </summary>
public class UploadFile_Single
{
/* web.config设置:
* <appSettings>
* <add key="uploadDir" value="UploadFiles/" />
* <add key="uploadFileExt" value="gif|jpg|jpeg|png|bmp|doc|xls|hlp|chm|rar|zip" />
* <add key="uploadFileSize" value="2048" />
* <add key="isUseRandFileName" value="true" />
* </appSettings>
*
* private string uploadDir = ConfigurationSettings.AppSettings["uploadDir"];;
* private string uploadFileExt = ConfigurationSettings.AppSettings["uploadFileExt"];
* private int uploadFileSize = int.Parse(ConfigurationSettings.AppSettings["uploadFileExt"]);
* private bool isUseRandFileName = bool.Parse(ConfigurationSettings.AppSettings["uploadFileExt"]);
* */
//上传目录,相对于本文件路径;以“/”结尾;如果目录不存在,会自动创建;如果要启用绝对路径,写法:“/虚拟目录名称/上传文件目录名称/”
private string uploadDir = "UploadFiles/";
//允许上传文件类型,逗号隔开
private string uploadFileExt = "gif|jpg|jpeg|png|bmp|doc|xls|hlp|chm|rar|zip";
//允许上传文件大小,k 为单位
private int uploadFileSize = 2048;
//是否使用随机上传文件名;如果不使用,会覆盖上传目录的同名文件
private bool isUseRandFileName = true;
//上传结果
private string uploadResult = "";
//上传文件保存路径
private string uploadPath = "";
public string UploadDir
{
get
{
return uploadDir;
}
set
{
uploadDir = value;
}
}
public string UploadFileExt
{
get
{
return uploadFileExt;
}
set
{
uploadFileExt = value;
}
}
public int UploadFileSize
{
get
{
return uploadFileSize;
}
set
{
uploadFileSize = value;
}
}
public bool IsUseRandFileName
{
get
{
return isUseRandFileName;
}
set
{
isUseRandFileName = value;
}
}
public string UploadResult
{
get
{
return uploadResult;
}
}
public string UploadPath
{
get
{
return uploadPath;
}
}
//上传文件方法
public bool Upload(Page page)
{
HttpPostedFile uploadFile = page.Request.Files[0];
//获取上传文件属性
string fileFullName = uploadFile.FileName;
string contentType = uploadFile.ContentType;
int contentLength = uploadFile.ContentLength;
//取得文件名
string fileName = System.IO.Path.GetFileName(fileFullName);
//取得扩展名
string fileExt = System.IO.Path.GetExtension(fileName).Remove(0, 1);
//检测文件类型
if( ("|"+UploadFileExt+"|").IndexOf( ("|"+fileExt+"|") ) < 0 )
{
uploadResult = "<font color='red'><b>上传文件格式错误!允许上传文件类型(" + uploadFileExt + ")</b></font>";
return false;
}
//检测文件大小
if(contentLength<1 || contentLength>uploadFileSize*1024)
{
uploadResult = "<font color='red'><b>上传文件大小错误!最大允许上传 " + uploadFileSize + " KB</b></font>";
return false;
}
//如果上传目录不存在,创建目录
if(!uploadDir.EndsWith("/")) uploadDir += "/";
string tempPath = "";
string tempMapPath = "";
string tempDir = uploadDir.Substring(0,1)!="/" ? uploadDir : uploadDir.Substring(uploadDir.IndexOf("/", 2)+1) ;
while(tempDir.IndexOf("/")>=0)
{
tempPath += tempDir.Substring(0, tempDir.IndexOf("/")+1);
tempMapPath = System.Web.HttpContext.Current.Server.MapPath(tempPath);
if(!Directory.Exists(tempMapPath))
{
Directory.CreateDirectory(tempMapPath);
}
tempDir = tempDir.Substring(tempDir.IndexOf("/")+1);
}
//随机文件名
if(isUseRandFileName)
{
fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + (new Random()).Next(100, 1000).ToString() + "." + fileExt;
}
string saveFilePath = System.Web.HttpContext.Current.Server.MapPath(uploadDir) + @"\" + fileName;
uploadFile.SaveAs(saveFilePath);
uploadPath = uploadDir+fileName;
//SaveUploadPathToDB(uploadPath);
uploadResult += "<font color='blue'><b>文件上传成功!</b></font>" + "<br/><br/>" +
"<b>保存位置:</b>" + uploadPath;
return true;
}
//将上传文件路径存入数据库
private void SaveUploadPathToDB(string uploadPath)
{
//string sql = "update table 1 set f_uploadpath='" & uploadPath & "' where ";
//TSQL.ExecuteNonQuery(sql, null);
}
}
}
调用:
UploadFile_Single upfile = new UploadFile_Single();
upfile.Upload(this);
upfile.Upload(this);
UploadFile_Single upfile = new UploadFile_Single();
upfile.UploadDir = "UploadFiles/";
upfile.UploadFileExt = "gif|jpg|jpeg|png|bmp";
upfile.UploadFileSize = 1024;
upfile.IsUseRandFileName = true;
upfile.Upload(this);
upfile.UploadDir = "UploadFiles/";
upfile.UploadFileExt = "gif|jpg|jpeg|png|bmp";
upfile.UploadFileSize = 1024;
upfile.IsUseRandFileName = true;
upfile.Upload(this);
UploadFile_Single upfile = new UploadFile_Single();
upfile.UploadDir = "/虚拟目录名称/UploadFiles/1/2/3/";
upfile.UploadFileExt = "gif|jpg|jpeg|png|bmp";
upfile.UploadFileSize = 1024;
upfile.IsUseRandFileName = true;
upfile.Upload(this);
Response.Write(upfile.UploadResult); //上传结果信息
Response.Write(upfile.UploadPath); //上传保存路径
(上传目录可以用绝对路径,也可用相对路径;如果上传目录不存在,会自动创建)upfile.UploadDir = "/虚拟目录名称/UploadFiles/1/2/3/";
upfile.UploadFileExt = "gif|jpg|jpeg|png|bmp";
upfile.UploadFileSize = 1024;
upfile.IsUseRandFileName = true;
upfile.Upload(this);
Response.Write(upfile.UploadResult); //上传结果信息
Response.Write(upfile.UploadPath); //上传保存路径