1

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;
            }
        }
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

1 Answers1

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)
   {
   }
}
gideon
  • 19,329
  • 11
  • 72
  • 113
  • It could be `doc` files or `pdf` files – Anyname Donotcare Apr 18 '12 at 09:01
  • 1
    @just_name Each specific file will be handled by a specific application. You could use `application/octet-stream` for any binary file and the user would get a download prompt and depending on the browser even a choice for which application to use. – gideon Apr 18 '12 at 09:06