Asp.Net Sending PDF to browser

13,261

Is the Content-Length being returned in the header actually correct for the file you're sending? I'm just comparing this to some production code we use here and it looks like we explicitly set the Content-Length header. If I recall correctly, some browsers have a problem if the header and the actual file size don't match.

Edit The question author found that changing the Content-Disposition header to application/download instead of application/pdf seems to work around the problem.

Share:
13,261
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I've been trying to get this aspx page to serve up a pdf. It works correctly in Firefox, but IE gives

    Internet Explorer cannot download getform.aspx from SERVER_NAME
    Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found.

    This is the general functionality of my code. It's spread across multiple functions (this is why we're not using WriteFile - sometimes we generate the pdf on the fly), but this is generally it:

    FileStream fs = File.Open(Path.Combine(PdfBasePath, "form.pdf"), FileMode.Open, FileAccess.Read);
    Stream output = Response.OutputStream;
    
    byte[] buffer = new byte[BUFFER_SIZE];
    int read_count = fs.Read(buffer, 0, BUFFER_SIZE);
    while (read_count > 0)
    {
        output.Write(buffer, 0, read_count);
        read_count = fs.Read(buffer, 0, BUFFER_SIZE);
    }
    
    fs.Close();
    
    Response.Clear();
    Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;
    Response.AddHeader("Content-Disposition", "attachment; filename=form.pdf");
    Response.Output.Flush();
    Response.End();
    

    Looking at Fiddler, the page is being fetched using this:

    GET /getform.aspx?Failure=Y&r=someencryptedstring HTTP/1.1

    It is being returned to the browser thus:

    HTTP/1.1 200 OK
    Date: Thu, 09 Apr 2009 22:08:33 GMT
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    X-AspNet-Version: 2.0.50727
    Pragma: no-cache
    Content-Disposition: attachment; filename=form.pdf
    Cache-Control: no-cache, no-store
    Pragma: no-cache
    Expires: -1
    Content-Type: application/pdf
    Content-Length: 628548

    This is really bugging me. I'm not using SSL, otherwise this KB article would seem to apply. Anyone have any ideas?