protected void UpFlies_Click(object sender, EventArgs e)
{
if (fuDocFile.HasFile)
{
string name = fuDocFile.PostedFile.FileName; // 客户端文件路径
System.IO.FileInfo file = new System.IO.FileInfo(name);
string fileName = file.Name; // 原文件名称
string upfileName; // 上传文件名称
string da = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();
string OrignFolder =DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString(); //文件保存的目录名称
string fileExtension = System.IO.Path.GetExtension(fileName); //取文件的扩展名
if (fileExtension == ".doc" || fileExtension == ".gif" || fileExtension == ".jpg" || fileExtension == ".rar" || fileExtension == ".zip" || fileExtension == ".pdf" || fileExtension == ".xls" || fileExtension == ".txt")
{
upfileName = da + fileExtension; //获取上传文件的文件名
if (!Directory.Exists(Server.MapPath("./UpFiles/") + OrignFolder)) //判断上传文件路径是否存在,若不存在,否则创建。
Directory.CreateDirectory(Server.MapPath("./UpFiles/") + OrignFolder); //创建目录
string webFilePath = Server.MapPath("./UpFiles/" + OrignFolder+"/" + upfileName); // 服务器端文件路径
fuDocFile.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
int strSize = fuDocFile.PostedFile.ContentLength / 1024; //上传文件的大小
string strPath = "UpFiles/" + fileName; //上传文件的路径
string typeurl = " ";
switch (fileExtension)
{
case ".gif": typeurl = "~/images/gif.gif"; break;
case ".jpg": typeurl = "~/images/jpg.gif"; break;
case ".rar": typeurl = "~/images/rar.gif"; break;
case ".zip": typeurl = "~/images/rar.gif"; break;
case ".pdf": typeurl = "~/images/pdf.gif"; break;
case ".doc": typeurl = "~/images/doc.gif"; break;
case ".xls": typeurl = "~/images/xls.gif"; break;
case ".txt": typeurl = "~/images/txt.gif"; break;
}
lbupdatemessage.Text = "上传文件的类型" + typeurl + "<br> 上传文件的大小:" + strSize + "(KB)<br> 上传文件的路径:" + strPath + "<br> 上传文件的原始名称:" + fileName + "<br>上传文件名:" + upfileName; ;
}
else
{
lbmesg.Visible = true;
}
}
else
{
Response.Write("<script language='javascript'>alert('请点击浏览,选择要上传的文件!')</script>");
}
}
转载于:https://www.cnblogs.com/zyming/archive/2008/10/25/1319483.html