protected void UploadBtn_Click(object sender, EventArgs e)
{
//******************获取文件的扩展名如:(.rar)***************************
string fileName = this.FileUpload1.PostedFile.FileName;//获取上传文件的名称
int length = fileName.Length;//获取上传文件的名称的长度
int intID = fileName.LastIndexOf(".");
string oldName = fileName.Substring(intID, length - intID);//获得文件的扩展名
//************************************************************************
bool fileOK = false;
string uploadPath = string.Empty;//上传文件的路径
string uploadInfo = UploadTB.Text;//文件说明
//获取根文件绝对路径
string path = Server.MapPath("~/UserRes/" + "1" + "//");
//如上传了文件,就判断文件格式
FileUpload FU = FileUpload1;
if (FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FU.FileName).ToLower();
string[] allowedExtensions ={ ".gif", ".jpg", ".png", ".bmp", ".zip", ".rar", ".doc", ".xls", ".docx", ".swf", };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
//判断文件是否过大
if (FileUpload1.PostedFile.ContentLength > 5242880) //获取上载文件的字节大小
{
Response.Write("");
return;
}
uploadPath = System.DateTime.Now.ToString("yyyyMMddhhmmss") + oldName;
//调用saveas方法,实现上传文件
if (fileOK)
{
try
{
FileUpload1.SaveAs(path + System.DateTime.Now.ToString("yyyyMMddhhmmss") + oldName);
Response.Write("");
UploadTB.Text = string.Empty;
}
catch(Exception error)
{
Response.Write("");
}
}
else
{
Response.Write("");
UploadTB.Text = string.Empty;
return;
}
}
阅读(1009) | 评论(0) | 转发(0) |