using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;public partial class Default3 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { ShowPic.Visible = false;//初始化不顯示 ShowText.Visible = false;//初始化不顯示 } protected void Button1_Click(object sender, EventArgs e) { if (Directory.Exists(Server.MapPath("~/upimg/hufu")) == false)//如果不存在就建立file檔案夾 { Directory.CreateDirectory(Server.MapPath("~/upimg/hufu")); } //Directory.Delete(Server.MapPath("~/upimg/hufu"), true);//刪除檔案夾以及檔案夾中的子目錄,檔案 //判斷檔案的存在 if (File.Exists(Server.MapPath("~/upimg/Data.html"))) { Response.Write("Yes"); //存在檔案 } else { Response.Write("No"); //不存在檔案 File.Create(MapPath("~/upimg/Data.html"));//建立該檔案 } string name = GetFiles.FileName;//擷取已上傳檔案的名字 string size = GetFiles.PostedFile.ContentLength.ToString();//擷取已上傳檔案的大小 string type = GetFiles.PostedFile.ContentType;//擷取已上傳檔案的MIME string postfix = name.Substring(name.LastIndexOf(".") + 1);//擷取已上傳檔案的尾碼 string ipath = Server.MapPath("upimg") +"\\"+ name;//擷取檔案的實際路徑 string fpath = Server.MapPath("upfile") + "\\" + name; string dpath = "upimg\\" + name;//判斷寫入資料庫的虛擬路徑 ShowPic.Visible = true;//啟用 ShowText.Visible = true;//啟用 //判斷檔案格式 if (name == "") { Response.Write("<script>alert('上傳檔案不可為空')</script>"); } else{ if (postfix == "jpg" || postfix == "gif" || postfix == "bmp" || postfix == "png") { GetFiles.SaveAs(ipath); ShowPic.ImageUrl = dpath; ShowText.Text = "你上傳的圖片名稱是:" + name + "<br>" + "檔案大小:" + size + "KB" + "<br>" + "檔案類型:" + type + "<br>" + "存放的實際路徑為:" + ipath; } else { ShowPic.Visible = false;//隱藏圖片 GetFiles.SaveAs(fpath);//由於不是圖片檔案,因此轉存在upfile這個檔案夾 ShowText.Text = "你上傳的檔案名稱是:" + name + "<br>" + "檔案大小:" + size + "KB" + "<br>" + "檔案類型:" + type + "<br>" + "存放的實際路徑為:" + fpath; } } }}