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 NetAdmin_APicture_UploadImg : System.Web.UI.Page ...{ protected void Page_Load(object sender, EventArgs e) ...{ } protected void btnUpload_Click(object sender, EventArgs e) ...{ lblMessage.Text = ""; lblMessage.Visible = false; System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; System.Text.StringBuilder strmsg = new System.Text.StringBuilder(""); string[] rd = Request.Form[1].Split(',');//獲得圖片描述的文字框字串數組,為對應的圖片的描述 string albumid=ddlAlbum.SelectedValue.Trim(); int ifile; for (ifile = 0; ifile < files.Count; ifile++) ...{ if (files[ifile].FileName.Length > 0) ...{ System.Web.HttpPostedFile postedfile = files[ifile]; if (postedfile.ContentLength / 1024 > 1024)//單個檔案不能大於1024k ...{ strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大於1024k<br>"); break; } string fex = Path.GetExtension(postedfile.FileName); if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF") ...{ strmsg.Append(Path.GetFileName(postedfile.FileName) + "---圖片格式不對,只能是jpg或gif<br>"); break; } } } if (strmsg.Length <= 0)//說明圖片大小和格式都沒問題 ...{ //以下為建立圖庫目錄 string dirname = "pic00" + ddlAlbum.SelectedValue.Trim(); string dirpath = Server.MapPath("http://www.iiwnet.com/php"); dirpath = dirpath + "" + dirname; if (Directory.Exists(dirpath) == false) ...{ Directory.CreateDirectory(dirpath); } Random ro = new Random(); int name = 1; for (int i = 0; i < files.Count; i++) ...{ System.Web.HttpPostedFile myFile = files[i]; string FileName = ""; string FileExtention = ""; string PicPath = ""; FileName = System.IO.Path.GetFileName(myFile.FileName); string stro=ro.Next(100,100000000).ToString()+name.ToString();//產生一個隨機數用於新命名的圖片 string NewName =DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+stro; if (FileName.Length > 0)//有檔案才執行上傳操作再儲存到資料庫 ...{ FileExtention = System.IO.Path.GetExtension(myFile.FileName); string ppath = dirpath + "" + NewName + FileExtention; myFile.SaveAs(ppath); string FJname = FileName; PicPath = "PicBase" + "" + dirname + "" + NewName + FileExtention; } AddPicture(PicPath, rd[i], albumid);//將圖片資訊儲存到資料庫 if (name == 1)//如果為每次更新的第一張圖片,則將它更新為象冊的封面 ...{ upFirstimg(albumid, PicPath); } name = name + 1;//用來重新命名規則的變數 } } else ...{ lblMessage.Text = strmsg.ToString(); lblMessage.Visible = true; } } private void AddPicture(string imgpath,string imgnote,string albumid) ...{ string sql = "insert WB_AlbumImges(ImgPath,ImgNote,AlbumID) values('"+imgpath+"','"+imgnote+"','"+albumid+"')"; DB mydb = new DB(); mydb.RunProc(sql); } private void upFirstimg(string albumid,string firstimg) ...{ string sql = "update WB_Album set FirstImg='"+firstimg+"' where AlbumID="+albumid; DB mydb = new DB(); mydb.RunProc(sql); } } |