protected void btn_Upload_Click(object sender, EventArgs e) { string serverPath = Server.MapPath("Upload"); if (!System.IO.Directory....
protected void btn_Upload_Click(object sender, EventArgs e) { string serverPath = Server.MapPath("Upload"); if (!System.IO.Directory.Exists(serverPath))//判斷文件的目錄是否存在 { System.IO.Directory.CreateDirectory(serverPath);//如果文件目錄不存在,創建文件目錄 } if (file_upload.HasFile)//判斷是否選擇了控制項 { int fileSize = this.file_upload.PostedFile.ContentLength / 1024 / 1024;//獲取上傳文件的大小 if (fileSize > 8)//如果上傳文件的大小大於8兆 { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('只允許上傳小於8兆的文件')", true); return; } else { file_upload.SaveAs(serverPath + "\\" + file_upload.FileName);//文件保存到伺服器的指定目錄下 Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('上傳文件成功!!')",true); } } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('請選擇要上傳的文件')", true); return; } }