分享代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
判斷文件大小#region 判斷文件大小
int intFileLength = this.FileUpload1.PostedFile.ContentLength;
if (intFileLength > 50000)
{
this.Label1.Text = "文件大於50k,不能上傳";
return;
}
#endregion
判斷保存的文件夾是否存在#region 判斷保存的文件夾是否存在
string strUpPath = @"upfile\" + System.DateTime.Now.ToShortDateString();// +@"\";
//文件夾不存在的時候,創建文件夾
if (!System.IO.Directory.Exists(Server.MapPath(strUpPath)))
{
System.IO.Directory.CreateDirectory(Server.MapPath(strUpPath));
}
string strUrl = Server.MapPath(strUpPath + @"\" + this.FileUpload1.FileName);
#endregion
//上傳文件
this.FileUpload1.SaveAs(strUrl);
this.Label1.Text = "文件上傳成功";
}
catch (System.Exception ex)
{
this.Label1.Text = "文件上傳失敗:" + ex.Message;
}
}