• C# extjs 图片、附件以二进制形式存储至数据库,图片显示,附件下载


    页面

    <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=" 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这个默认图片

    虽然知道往数据库中存个路径更好一些,但是领导要求就做了,在这里记一下先。

  • 相关阅读:
    读GNU官方的Make manual
    GNU LD之一LMA和VMA
    GNU LD之二LD script
    gcc 库的链接顺序问题
    GDB远程连接RX Probe在线debug程序
    mips gnu工具使用
    read PSE TPS2384 POE Firmware Guide
    adult道具项目开发
    2、与网络相关的命令 netstat , tcpdump 等。
    1、与 CPU、 内存、 磁盘相关的命令
  • 原文地址:https://www.cnblogs.com/wningning/p/2990621.html
Copyright © 2020-2023  润新知