How to open files stored in bytes in a database using the http handler?

I have my files stored in bytes in my database.

How to open such files using your application (for example, 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
source share
1 answer

You must write it to the response stream with the correct MIME type. You can find a list of MSI MIME file formats here for pdf this application/pdf. If you want to keep it shared (all binaries) Useapplication/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)
   {
   }
}
+1
source

All Articles