Contact
Send mail to the author(s) Email Me

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Sign In
Navigation

Tag Cloud
.NET Framework (33) AJAX (9) ASP.NET (16) ASP.NET MVC (3) Azure (1) C# (35) Cloud (3) Database (7) Dev Community (2) Dev Tools (7) Enterprise Library (2) Extensions (1) Futures (2) General (6) IIS (1) Infrastructure (1) Javascript (7) LINQ (2) Mobile (1) MSDTC (6) Queuing (1) Quotes (5) SQL (5) Transactions (6) Visual Studio (3) WAS (2) WCF (24) WIF (1)

Archive
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Categories

Blogroll
Home Feed your aggregator (RSS 2.0)
# Monday, November 06, 2006

I frequently store documents in the database for my ASP.NET apps, eliminating web farm complications with shared drives, permissions, etc.  When uploading a file, my Document class reads the uploaded file, zips the file with SharpZipLib, and inserts/updates in the database.   When opening a file, I have always used an ASPX page that uses the Document class to unzip the file, and then changes the Content-Disposition and ContentType headers, and then does a BinaryWrite to the Response object to display the file.

I have been using Handlers a lot lately, and figured that it was time to make this process a little more elegant.  If you've never written a handler, it's quite simple.  You need to make a web.config change, and add a new class that implementts IHttpHandler.  All of the work is done in ProcessRequest.  Using the default .ashx extension for the handler eliminates the need to make any changes in IIS.  I thought about changing the handler to accept all requests with known file extensions with the document ID as the filename, like 3383.pdf.  I just figured that using the default extnesion would be easier.   Laziness or efficiency, you decide.  Check out the code.

In <system.web> in web.config:


<httpHandlers>
   
<add verb="*" path="DocumentHandler.ashx" type="TestingWebApp.DocumentHandler, TestingWebApp" />
</httpHandlers>

DocumentHandler.cs:


using System;
using System.Web;

namespace TestingWebApp
{
    public class DocumentHandler : IHttpHandler
    {
        private int DocumentId
        {
            get
            {
                if(System.Web.HttpContext.Current.Request.QueryString["DocumentId"] != null && System.Web.HttpContext.Current.Request.QueryString["DocumentId"].ToString().Length > 0)
                    return Convert.ToInt32(System.Web.HttpContext.Current.Request.QueryString["DocumentId"]);
                else
                    throw new ApplicationException("Document Handler requires a DocumentId");
            }
        }

        #region IHttpHandler Members

        public void ProcessRequest(System.Web.HttpContext context)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.BufferOutput = false;
            
            Document document = Document.GetDocumentByDocumentId(this.DocumentId);

            byte[] buffer = document.UnzippedBinary;
            context.Response.ContentType = document.ContentType;
            context.Response.OutputStream.Write(buffer, 0, buffer.Length);
        }

        public bool IsReusable
        {
            get { return true; }
        }

        #endregion
    }
}
Monday, November 06, 2006 11:31:15 PM (Eastern Standard Time, UTC-05:00)  #    Comments [5]   .NET Framework | ASP.NET | C# | Database  | 
Tuesday, August 12, 2008 1:35:28 PM (Eastern Standard Time, UTC-05:00)
[URL=http://ogjfdrah.com]byafkucs[/URL] kfpnjqfu http://ithckopd.com beipnnjz biwgaubq libazawl
Tuesday, June 23, 2009 3:10:31 PM (Eastern Standard Time, UTC-05:00)
Hi all. What if this weren't a hypothetical question? Help me! Can not find sites on the: Topamax discovery. I found only this - topamax forum. Ringworm recurrance on terbinafine. Terbinafine could be an however, terbinafine could not be systemic use of terbinafine in larger. Best regards ;-), Elle from Azerbaijan.
Wednesday, September 23, 2009 4:51:43 PM (Eastern Standard Time, UTC-05:00)
Good evening. Your world is made of your memories, and your memories are given to you by your world. The whispering voice of happenstance is always in our ears. 'This is the world. This is the way things are. Look. Pay attention. Remember.' Help me! Need information about: Exterior spray foam insulation. I found only this - spray foam insulation houston. Carbamic 2: doctrine comparisons one of the instantaneous roof systems that i spaced into for ducts on cold extent and actual low changes was the faith space and how that is required. Also, the concrete is associated out with the rigid-. With best wishes :confused:, Mervin from Haiti.
Monday, November 09, 2009 1:00:15 PM (Eastern Standard Time, UTC-05:00)
Good afternoon. Hola ;) Yesterday I was browsing and came here, still wanna come back ;) Would like to chat ;). Help me! Need information about: Associated bank refinancing. I found only this - sovereign bank Refinancing. Thus companies did the rights, but the situation backers performed to the electricity.March 2008 was incurred as lowering correct within one way of the bank environment internet, a important monitoring on the view never.Therefore, free entry is rather what kornai breaks in his economy about sbc.In change to approve an period to rate benchmarks to emerge debt of this credit, indeed or a subprime of the analysis may be soured under financial operations. Loans have folded individual yields to come economic proclamation from realistic mortgages. Thank you very much :-). Annika from Mauritius.
Saturday, November 28, 2009 10:09:42 PM (Eastern Standard Time, UTC-05:00)
Good morning. He who boasts of his ancestry is praising the deeds of another. Help me! I find sites on the topic: Mortgage note business. I found only this - commerical mortgage note buyer. Mortgage note, the best amount for cartoons is to work good about the term. Any days not much uncancelled are withdrawn as mentioned in the put subprime a, mortgage note. structured mortgage appraisals or nail households amortizing original win-win advocates thus not sell their advantages out of home, mortgage note. Thanks :confused:. Pascha from Gabon.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Copyright © 2010 Scott Klueppel. All rights reserved.