tags:

views:

369

answers:

2

I have a website that has several PDF files. I need to have quite a few of them locked down with the standard ASP.NET authentication (in a folder with web.config that denies anonymous users).

I set PDF files to get handled by the ASP.NET worker process and added:

<add type="System.Web.StaticFileHandler" path="*.pdf" verb="*" />

to my web.config, but for some reason they hang when downloading. I've seen this issue before on an old server, and for the live of me I can't remember what I did to solve it. Does anyone have any idea?

Thanks.

A: 

I would open fiddler and see what requests were actually being sent to the server and what the responses are. It may also depend on how you are actually requesting (ie the path) and how you are serving the PDF. I demonstrate how to return password protected PDF documents in one of my WROX eBooks and in my handlers and modules presenation I do at user groups. http://professionalaspnet.com/archive/2008/08/10/CodeStock-Rocked_2100_.aspx (link to download the code on the page).

Here is the code I use to return a PDF using a Handler, it might help you out.

    'First run the supplied filename through a URL decoder to change any URL
    'characters back to their normal selves.
    sDocument = HttpUtility.UrlDecode( _
        Path.GetFileName(context.Request.Url.ToString))
    'Now build a full physical path to the file on the server.
    sDocument = Path.Combine(Path.Combine( _
        context.Request.PhysicalApplicationPath, "PDF"), sDocument)

    'Verify we actually have the file to serve.
    If File.Exists(sDocument) = False Then
        context.Server.Transfer("badpdf.aspx")
    End If

        'The next two sections are from Scott Hanselman's blog, but seem to be out of date, 
    'since it was posted in 2003
    'http://www.hanselman.com/blog/InternetExplorerAndTheMagicOfMicrosoftKBArticleQ293792.aspx
    'This is done to support older browsers that were not as efficient
    'at managing document requests.
    If InStr(1, context.Request.UserAgent, "contype") > 0 Then
        'Just send the mime/type
        Response.ContentType = "application/pdf"
        Response.End()
        Exit Sub
    End If

    Dim Language As String = context.Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")
    If String.IsNullOrEmpty(Language) Then
        Response.Clear()
        Response.ContentType = "application/pdf"
        Response.AddHeader("Last-modified", "Mon, 01 Sep 1997 01:03:33 GMT")
        Response.Status = "304 Not Modified"
        Response.End()
        Exit Sub
    End If

    'Set the Cacheability
    Response.Cache.SetCacheability(HttpCacheability.Public)
    Response.Cache.SetExpires(DateTime.MinValue)
    Response.ContentType = "application/pdf"

    'This opens the Open/Save Dialog
    Response.AddHeader("Content-Disposition", "attachment; filename=" & _
     Path.GetFileName(sDocument))

    'This Bypasses the Open/Save Dialog
    'Response.AddHeader("Content-Disposition", "inline; filename=" & _
    ' Path.GetFileName(sDocument))

    If File.Exists(sDocument) Then
        'Write the file to the Response Output stream without buffering.
        'This is new to ASP.NET 2.0. Prior to that use WriteFile.
        Response.TransmitFile(sDocument)
        Response.End()
    End If
Chris Love
A: 

I think I finally figured out what I was missing. I needed to set the MIME type for PDF files to application/octet-stream instead of application/pdf.

Ryan Smith