C# extjs 圖片、附件以二進位形式儲存至資料庫,圖片顯示,附件下載

來源:互聯網
上載者:User

頁面

<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這個預設圖片

雖然知道往資料庫中存個路徑更好一些,但是領導要求就做了,在這裡記一下先。

 

 

 

 

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.