0

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

FaNIX
  • 1,888
  • 6
  • 33
  • 60
  • What behavior are you seeing in IE7/8? – RoccoC5 Feb 07 '12 at 23:32
  • When I click on the LinkButton, nothing happens... No download file dialog. – FaNIX Feb 07 '12 at 23:49
  • To ignore the ThreadAbortException when calling Response.End() you would need to add a try catch and ignore that exception. try { Response.End(); } catch(Exception ex) { if(ex is ThreadAbortException) { // do nothing } else { throw; } } – Jim Scott Feb 08 '12 at 00:09

3 Answers3

2

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();
DotNetUser
  • 6,494
  • 1
  • 25
  • 27
  • Your code works perfectly in all browsers, but still not working in IE7 or IE8. File download dialog still not being displayed :( – FaNIX Feb 08 '12 at 01:39
  • Can you try using window.open ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectScript", string.Format("window.open('{0}','_blank');",handlerUrl), true); inplace of window.location where handlerUrl is your ashx url – DotNetUser Feb 08 '12 at 02:10
1

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

Community
  • 1
  • 1
Etch
  • 3,044
  • 25
  • 31
  • 1
    I tried removing the Cache-control header and same thing happens... which is nothing. – FaNIX Feb 07 '12 at 23:40
1

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;
}
RoccoC5
  • 4,185
  • 16
  • 20
  • Thanks, the window.location is a way better way of doing it than using an iFrame. It still doesn't fix the problem but I do think it's an enhancement. Thanks – FaNIX Feb 08 '12 at 01:29