上传后的文件有两种命名方式:
1、使用原文件名,若已存在就在文件名后加序号
2、使用时间+随机数作文件名
HtmlUploadFile.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HtmlUploadFile.aspx.cs" Inherits="ZWY.Web.HtmlUploadFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<input type="file" name="htmlFile" />
<asp:Button ID="btnSave" runat="server" Text="上传" οnclick="btnSave_Click" />
</div>
</form>
</body>
</html>
HtmlUploadFile.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace ZWY.Web
{
public partial class HtmlUploadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
ZWY.Common.HtmlUploadFile htmlUploadFile = new ZWY.Common.HtmlUploadFile();
HttpPostedFile myFile = Request.Files["htmlFile"];
htmlUploadFile.FileRenameMethod = 0;
if (htmlUploadFile.HTMLUpLoad(myFile))
{
Response.Write("上传成功:" + htmlUploadFile.FilePath);
}
else
{
Response.Write(htmlUploadFile.Message);
}
}
}
}
上传文件类HtmlUploadFile.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
namespace ZWY.Common
{
public class HtmlUploadFile
{
private string extensionLim = ".jpg|.gif";
private int fileLengthLim = 1024;//单位是KB
private string filePath = "";
private int fileRenameMethod = 0;//文件重命名方法,0:使用原名,1:时间+三位随机数
private string message = "";
private string uploadDir = "~/upload/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString(); //上传目录:/upload/yyyy/MM;
public bool HTMLUpLoad(HttpPostedFile htmlFile)
{
string originalFilePath = "";//上传文件的路径
string originalFileName = "";//上传文件的文件名,不带扩展名
string fileExtension = "";//上传文件的扩展名
string fileName = "";//上传后的新文件名
if (htmlFile.FileName.Length > 0 && htmlFile.ContentLength > 0)
{
originalFilePath = htmlFile.FileName;
originalFileName = Path.GetFileNameWithoutExtension(originalFilePath);
fileExtension = Path.GetExtension(originalFilePath);
if (this.extensionLim.IndexOf(fileExtension.ToLower()) < 0)
{
this.message = "不能上传该类型文件(" + originalFilePath + "),您只能上传以下文件:" + this.extensionLim;
return false;
}
if (htmlFile.ContentLength > this.fileLengthLim * 1024)
{
this.message = "上传的文件(" + originalFilePath + ")大于最大限制:" + this.fileLengthLim + "KB";
return false;
}
string saveDir = HttpContext.Current.Server.MapPath(uploadDir);
if (!Directory.Exists(saveDir))
{
Directory.CreateDirectory(saveDir);
}
if (fileRenameMethod == 0)
{
//使用原文件名,如果文件名已存在,就在文件名后加序号
fileName = originalFileName + fileExtension;
if (File.Exists(saveDir + @"\" + fileName))
{
int i=1;
while (File.Exists(saveDir + @"\" + originalFileName + "_" + i.ToString() + fileExtension))
{
i++;
}
fileName = originalFileName + "_" + i.ToString() + fileExtension;
}
}
else if (fileRenameMethod == 1)
{
//使用时间+随机数重命名文件
string strDateTime = DateTime.Now.ToString("yyMMddhhmmssfff");//取得时间字符串
Random ran = new Random();
string strRan = Convert.ToString(ran.Next(100, 999));//生成三位随机数
fileName = strDateTime + strRan + fileExtension;
}
string savePath = saveDir + @"\" + fileName;
htmlFile.SaveAs(saveDir + @"\" + fileName);
if (this.uploadDir.Substring(0, 1) == "~")
{
this.uploadDir = this.uploadDir.Substring(1, this.uploadDir.Length - 1);
}
this.filePath = this.uploadDir + fileName;
return true;
}
else
{
message = "请选择上传文件";
return false;
}
}
public string ExtensionLim
{
get
{
return this.extensionLim;
}
set
{
this.extensionLim = value;
}
}
public int FileLengthLim
{
get
{
return this.fileLengthLim;
}
set
{
this.fileLengthLim = value;
}
}
public string FilePath
{
get
{
return this.filePath;
}
set
{
this.filePath = value;
}
}
/// <summary>
/// 上传文件重命名方法,0:使用原文件名,若已存在就在文件名后加序号,1:使用时间+随机数作文件名
/// </summary>
public int FileRenameMethod
{
get
{
return fileRenameMethod;
}
set
{
fileRenameMethod = value;
}
}
public string Message
{
get
{
return this.message;
}
}
public string UploadDir
{
get
{
return this.uploadDir;
}
set
{
this.uploadDir = value;
}
}
}
}