頁面
<ext:FileUpload runat="server" Label="附件" ID="attachment" Width="200px"></ext:FileUpload><ext:FileUpload runat="server" Label="照片" ID="photo" Width="200px"></ext:FileUpload>
後台附件添加
if (System.IO.Path.GetExtension(attachment.FileName).ToLower() != null&&System.IO.Path.GetExtension(attachment.FileName).ToLower() != "") { string attachmentname = System.IO.Path.GetFileName(attachment.FileName); string fileAttachment = System.IO.Path.GetExtension(attachment.FileName).ToLower(); string[] allowAttachment = { ".jpg", ".gif", ".docx", ".pptx", ".txt", ".xsl", ".rar", ".doc", ".zip", ".png", ".jpeg", ".bmp" }; bool bl = false; for (int i = 0; i < allowAttachment.Length; i++) { if (fileAttachment == allowAttachment[i]) { bl = true; break; } } if (!bl) { Alert.ShowInTop("請上傳符合格式的附件"); return; } int AttachmentLength = attachment.PostedFile.ContentLength; //本文的大小 Byte[] FileByteArrayAtt = new Byte[AttachmentLength]; //圖象檔案臨時儲存Byte數組 Stream StreamObjectAtt = attachment.PostedFile.InputStream; //建立資料流對像 //讀取圖象檔案資料,FileByteArray為資料儲存體,0為資料指標位置、FileLnegth為資料長度 StreamObjectAtt.Read(FileByteArrayAtt, 0, AttachmentLength); //將數組從第0位到數組的長度位元據都讀取進StreamObjectAtt hrmsfile.Attachment = FileByteArrayAtt; hrmsfile.Attachmentformat = attachmentname; }//然後將hrmsfile這個new出來的實體存至資料庫即可
附件下載頁面
<table><tr> <td><strong>附件:</strong></td> <td><a href="FuJianXiazhai.ashx?id=<%# Eval("id")%>"> <%#Eval("Attachmentformat")%></a></td> </tr></table>
FuJianXiaZhai.ashx頁面
string id = context.Request.QueryString["id"]; HDQY.HDQYMS.Business.MsHrms.Hrms_fileBLL bll = new HDQY.HDQYMS.Business.MsHrms.Hrms_fileBLL(); HDQY.HDQYMS.Entity.HrmsFile ra = bll.GetHrmsFileByID(id); if (ra != null && ra.Attachment != null) { context.Response.Clear(); string strFileName = ra.Attachmentformat; context.Response.ContentType = "APPLICATION/OCTET-STREAM"; context.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(ra.Attachmentformat)); context.Response.Buffer = true; context.Response.BinaryWrite(ra.Attachment); context.Response.Flush(); context.Response.Close(); }
照片的上傳和附件時一樣的,顯示在前台時頁面是:
<td rowspan="4" style="width: 100px;"><img alt="個人照片" height="120px" width="100px" src='Handler.ashx?id=<%# Eval("id")%>'></td>
同樣是向一個ashx檔案要圖片
public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; string id = context.Request.QueryString["id"]; if (string.IsNullOrEmpty(id)) { System.IO.FileStream fs = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath("http://www.cnblogs.com/icon/20111130160358207106.png"), System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] image = new byte[fs.Length]; fs.Read(image, 0, image.Length); context.Response.ContentType = "png";//設定輸出檔案類型 context.Response.OutputStream.Write(image, 0, image.Length); context.Response.End(); return; } HDQY.HDQYMS.Business.MsHrms.Hrms_fileBLL bll = new HDQY.HDQYMS.Business.MsHrms.Hrms_fileBLL(); HDQY.HDQYMS.Entity.HrmsFile hrmsfile = bll.GetHrmsFileByID(id); if (hrmsfile != null && hrmsfile.Photo != null && hrmsfile.Photo.Length != 0) { context.Response.ContentType = hrmsfile.Photoformat;//設定輸出檔案類型 context.Response.OutputStream.Write(hrmsfile.Photo, 0, hrmsfile.Photo.Length); context.Response.End(); } else { System.IO.FileStream fs = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath("http://www.cnblogs.com/icon/20111130160358207106.png"), System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] image = new byte[fs.Length]; fs.Read(image, 0, image.Length); context.Response.ContentType = "png";//設定輸出檔案類型 context.Response.OutputStream.Write(image, 0, image.Length); context.Response.End(); }//無圖片時給了20111130160358207106.png這個預設圖片
雖然知道往資料庫中存個路徑更好一些,但是領導要求就做了,在這裡記一下先。