locked
HTTPHandler RRS feed

  • Question

  • User1089038237 posted

    Currently I am working on ASP.Net 2.0 project. I have a need to display files (pdf,txt,doc,xls,gif,jpg) files stored in SQL Server 2005 in frontend. I use HTTP Handler and wrote the code below. I added httphandler item to my project and coded as below. Apart from that didn't do anything. When doing a request for the file to be displayed in the handler I get a prompt for UserName & Password.

    public class Handler : IHttpHandler {
        private static bool ThumbnailCallback()
        {
            return false;

        }      
        public void ProcessRequest (HttpContext context)
        {
            try
            {
                string id = context.Request["id"];
                string filetype = context.Request["filetype"];
                if (id != null)
                {
                    int PictureCol = 0;
                    //Database db = DatabaseFactory.CreateDatabase("Con");
                    DbCommand cmd = db.GetStoredProcCommand("spS_SiteDoc_SiteDocId");
                    db.AddInParameter(cmd, "@SiteDocId", DbType.Int64,id);
                    db.AddInParameter(cmd, "@UserId", DbType.String, "");
                    IDataReader dr = db.ExecuteReader(cmd);
                    dr.Read();
                    Byte[] file = new Byte[(dr.GetBytes(PictureCol, 0, null, 0, int.MaxValue))];

                    string contentType = "";
                   
                    switch (filetype.ToLower())
                    {
                        case ".gif":
                            contentType = "image/gif";
                            break;
                        case ".jpg":
                            contentType = "image/jpeg";
                            break;
                        case ".doc":
                           contentType = "doc";
                           break;
                       case ".txt":
                           contentType = "txt";
                           break;
                       case ".pdf":
                           contentType = "pdf";
                           break;                       
                   }
                   context.Response.ContentType = contentType;
                    context.Response.OutputStream.Write(file, 0, file.Length);
                    context.Response.Close();
                }
            }
            catch (Exception ex)
            {
               
                throw new Exception(ex.Message);
            }
        }
        public bool IsReusable {
            get {
            return false;
        }
      }
    }

     

    Please do help me  [:'(]

    Monday, June 11, 2007 5:18 AM

Answers

  • User1089038237 posted

    Created a Handler dll by opening Visual Studio C# and wrote the following code
    using System;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    namespace MyHandler
    {
        public class HttpFileHandler : IHttpHandler
        {
            private static bool ThumbnailCallback()
            {
                return false;
            }
            public void ProcessRequest(HttpContext context)
            {
                try
                {
                    string id = context.Request["id"];
                    string filetype = context.Request["filetype"];
                    //string UserId = context.Request["UserId"];
                    if (id != null)
                    {
                        SqlConnection con = new SqlConnection("Your connection");
                        SqlCommand cmd = new SqlCommand("SELECT varbinDoc from tblSiteDoc where intSiteDocId = " + id, con);
                        //SqlCommand cmd = new SqlCommand("spS_SiteDoc_SiteDocId", con);
                        //cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        //cmd.Parameters.Add(new SqlParameter("@SiteDocId", System.Data.SqlDbType.Int, 999999999, id));
                        //cmd.Parameters.Add(new SqlParameter("UserId",System.Data.SqlDbType.VarChar,50));
                        string contentType = "";
                        switch (filetype.ToLower())
                        {
                            case "gif":
                                contentType = "image/gif";
                                break;
                            case "jpg":
                                contentType = "image/jpeg";
                                break;
                            case "doc":
                                contentType = "application/doc";
                                break;
                            case "txt":
                                contentType = "text/plain";
                                break;
                            case "pdf":
                                contentType = "application/pdf";
                                break;
                            case "xls":
                                contentType = "application/excel";
                                break;
                        }
                        con.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        dr.Read();
                        context.Response.ContentType = contentType;
                        context.Response.BinaryWrite((Byte[])dr[0]);
                        con.Close();
                        context.Response.End();
                    }
                }
                catch (Exception ex)
                {

                    throw new Exception(ex.Message);
                }
            }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    Compiled this into a dll.
    Added this dll to my web application
    Added the following HTTPHandler tags to my web config file of the application
          <add verb="*" path="Test.txt" type="MyHandler.HttpFileHandler, MyHandler" />
          <add verb="*" path="Test.doc" type="MyHandler.HttpFileHandler, MyHandler" />
          <add verb="*" path="Test.pdf" type="MyHandler.HttpFileHandler, MyHandler" />
          <add verb="*" path="Test.xls" type="MyHandler.HttpFileHandler, MyHandler" />
          <add verb="*" path="Test.image" type="MyHandler.HttpFileHandler, MyHandler" />
    IIS ---> Your virtual Directory ---> rightclick properties ----> Virtual Directory   -----------------> Configuration -------------> Add
    Browse for aspnet_isapi.dll, Extension .doc, Limit GET,POST,HEAD,DEBUG and untick verify that file exists. Follow the same for .xls,.image,.pdf,.doc.

    Add a page to your application which displays the links to open the files <a href-"Test.pdf"?id=1>View File</a> 

     By doing so the HTTPHAndler handler opens the files with their own application

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 12, 2007 8:18 AM