How to open files stored as bytes in database with http handler?

烈酒焚心 提交于 2019-12-13 17:40:28

问题


I have my files stored as bytes in my database.

How to open files like this with its application (like microsoft office, acrobat reader ,...etc) or download it.

I want to do this with generic handler :


 public class Attachement : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                byte[] Attachement = (byte[])AttachementDAL.ReadAttachement(int.Parse(context.Session["attch_serial"].ToString())).Rows[0]["attach_content"];

            }
            catch (Exception ee)
            {
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

回答1:


You would write it to the response stream with the correct MIME type. You can find a list of MS file format MIME types here, for pdf it is application/pdf. If you want to keep it generic (all binary files) Use application/octet-stream

 public void ProcessRequest(HttpContext context)
 {
   try
   {
     byte[] Attachement = 
           (byte[])AttachementDAL
                  .ReadAttachement(
                      int.Parse(context.Session["attch_serial"].ToString())
                   ).Rows[0]["attach_content"];

     context.Response.Clear();
     context.Response.ContentType = "application/msword";
     foreach(byte b in Attachement)
     {
       context.Response.OutputStream.WriteByte(b);
     }
     context.Response.OutputStream.Flush();
   }
   catch (Exception ee)
   {
   }
}


来源:https://stackoverflow.com/questions/10206011/how-to-open-files-stored-as-bytes-in-database-with-http-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!