frankclaassens
frankclaassens

Reputation: 1930

IE7/8: PDF file won't download with HTTP GET

I'm using a DataGridView to display a list of statements. One of the columns is a LinkButton, which allows you to download that specific statement in PDF format. My code works perfectly in ALL BROWSERS, except for IE7 & IE8. I have no idea why that is.

       <asp:GridView ID="dgvEStatements" runat="server" EnableSortingAndPagingCallbacks="False"
            EnableViewState="true" GridLines="Vertical" Width="100%" AutoGenerateColumns="False"
            CssClass="gridheader" EmptyDataText="<%$ Resources:IBEStatements, dgvEStatements_NoRows %>"
            OnPageIndexChanging="dgvEStatements_PageIndexChanging" OnRowCommand="dgvEStatements_RowCommand"
            OnRowDataBound="dgvEStatements_RowDataBound">
            <Columns>
                <asp:BoundField DataField="Date" HeaderText="<%$ Resources:IBEStatements, dgvEStatements_DateHeader %>"
                    HeaderStyle-CssClass="lhs">
                    <ItemStyle CssClass="lhs" />
                </asp:BoundField>
                <asp:BoundField DataField="Description" HeaderText="<%$ Resources:IBEStatements, dgvEStatements_DescriptionHeader %>"
                    HeaderStyle-CssClass="lhs" />
                <asp:BoundField DataField="DocumentType" Visible="false" HeaderText="<%$ Resources:IBEStatements, dgvEStatements_DocumentTypeHeader %>"
                    HeaderStyle-CssClass="lhs">
                    <ItemStyle CssClass="lhs" />
                </asp:BoundField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkDownloadEStatement" runat="server" Text="<%$ Resources:IBEStatements, lnkDownloadEStatement %>" />
                    </ItemTemplate>
                    <ItemStyle CssClass="rhs" />
                </asp:TemplateField>
            </Columns>

        </asp:GridView>

The RowDataBound event for the Grid does the following:

protected void dgvEStatements_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton lnkEStatement = (LinkButton)e.Row.FindControl("lnkDownloadEStatement");

        string fileId = DataBinder.Eval(e.Row.DataItem, "StatementID").ToString();
        lnkEStatement.Attributes.Add("onclick", "javascript:EStatementDownload('" + fileId + "'); return false;");
    }        
}

Javascript function to call the page that creates the PDF:

function EStatementDownload(fileid) {
    var iframe = document.createElement("iframe");
    iframe.src = "EStatementFile.ashx?fileid=" + fileid;
    iframe.style.display = "none";
    document.body.appendChild(iframe);
}

Finally, the code behind for EStatementFile.ashx looks like this:

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            string args = context.Request.QueryString["fileid"].ToString();

            int statementID = 0;
            int.TryParse(args, out statementID);

            string documentID = String.Empty;
            string accountnumber = String.Empty;
            DateTime fileDate = DateTime.MinValue;

            foreach (EStatement item in EStatementListing.EStatements)
            {
                if (statementID == item.StatementID)
                {
                    documentID = item.DocumentID;
                    accountnumber = item.AccountNumber;
                    fileDate = item.DocumentDate;
                    break;
                }
            }

            EStatementFacade estatementFacade = new EStatementFacade();
            EStatement estatement = estatementFacade.GetEStatement(documentID, accountnumber, fileDate);
            if (estatement.Document != null)
            {
                context.Response.Clear();
                context.Response.ContentType = "Application/pdf";
                context.Response.Cache.SetCacheability(HttpCacheability.Private);
                context.Response.AppendHeader("Cache-Control", "private; must-revalidate");
                context.Response.AppendHeader("Pragma", "private");
                context.Response.AddHeader("content-disposition", "attachment; filename=" + fileDate.ToString("ddMMyyyy") + ".pdf");
                context.Response.BinaryWrite(estatement.Document);
                context.Response.Flush();                                      
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            context.ApplicationInstance.CompleteRequest();
        }
    }

When the Linkbutton on the grid is clicked, the following javascript information is displayed in Firebug, which might be useful in finding the issue: Firebug Output

Something interesting to note, if I call context.Response.End() directly after context.Response.Flush(), I get the following exception. Now the file download dialog is still being displayed in all browsers regardless of the exception, but in IE7 & IE8, still no download dialog.

context.Response.End(); 'context.Response.End()' threw an exception of type 'System.Threading.ThreadAbortException' base {System.SystemException}: {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.} ExceptionState: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

It might have something to do with the iFrame?

enter image description here

PS: Save the last image to see large image

Upvotes: 0

Views: 2184

Answers (3)

DotNetUser
DotNetUser

Reputation: 6612

Can you try this-

    Response.Buffer = true;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader(
              "Content-Disposition",
              string.Format("attachment; filename={0}",filename)
            );
            // stream pdf bytes to the browser
            Response.OutputStream.Write(estatement.Document, 0, estatement.Document.Length);
            Response.End();

Upvotes: 2

RoccoC5
RoccoC5

Reputation: 4213

Two things:

1) Clear all of your headers prior to creating your response in your handler. This solved an issue for me cause by Microsoft Security Bulletin MS11-100 where the Cache-Control header was getting set to no-cache="Set-Cookie" (see this blog post for more info) :

// snip...

if (estatement.Document != null)
{
    context.Response.ClearHeaders();
    context.Response.Clear();
    context.Response.ContentType = "Application/pdf";
    // snip...

2) I'm not sure if this is actually causing any issues, but rather than creating an iframe each time a user downloads a PDF, why not just set the window.location property? This way you're not adding "throw-away" iframes to the document, and the behavior should still be the same:

function EStatementDownload(fileid) {
    window.location = "EStatementFile.ashx?fileid=" + fileid;
}

Upvotes: 1

Etch
Etch

Reputation: 3054

Here is a post on stackoverflow that I had which sounds like the same problem.

IE8 and lower cant handle the Cache-control header and causes static content such as PDF's to not get downloaded.

Link

Upvotes: 1

Related Questions