將本地圖片上傳儲存到資料庫(理論上支援各種檔案的上傳)
阿新 • • 發佈:2019-02-16
//取得檔案的具體大小 int doclen = this.File1.PostedFile.ContentLength; //設定快取的具體大小 byte[] docBuffer = new byte[doclen]; //將要上傳的檔案讀取到快取中 Stream objStream; objStream = this.File1.PostedFile.InputStream; objStream.Read(docBuffer, 0, doclen); //得到檔案字尾名 string type = this.File1.PostedFile.FileName; string[] typeTemp = type.Split('.'); type = typeTemp[typeTemp.Length - 1]; //把要上傳的圖片顯示到頁面上 Response.BinaryWrite(docBuffer); //下面就是儲存到資料庫上了 MODEL.Mail.AccessoriesInfo ai = new MODEL.Mail.AccessoriesInfo(); ai.A_Content = docBuffer; ai.A_Date = DateTime.Now; ai.A_IsDelete = false; ai.A_Name = this.File1.PostedFile.FileName; ai.A_Size = doclen; ai.A_Type = type; ai.A_Users_Id = 1; DAL.SQLHelper.DefaultConnectString = COMMON.GlobalData.GetConnectionString(); if (DAL.DB.Mail_Accessories.Insert(ai.GetValues()) == 1) { COMMON.Message.Show(this, "成功!"); } else { COMMON.Message.Show(this, "失敗!"); }