Sneaking around a download problem with an iFrame

We have a download process where a user can select multiple files across multiple tabs of a Telerik control. They click the wonderful download button, and the files magically appear! It’s very Harry Potter-like. The problem is that somewhere along the way, a Voldamort-like bug was causing problems. The first download would work, but if the user tried to download more files, then weird things would happen, like the checkboxes disappearing, menus being sucked into a void, and the whole thing just didn’t work.

My solution was to quit programming and become a travel writer. But, since that seemed like a radical solution to my problem, I returned to my desk and tried this. First, I created an iFrame called downloadFrame:

<iframe id=”downloadFrame” runat=”server” />

So after the code processes the download, pulling the data from our various systems and storing them as binary in a Session variable, Session[“FileContents”]. And in the Page_PreRender event, the iFrame src attribute is set to the page that actually does the work.

protected void Page_PreRender(object sender, EventArgs e)
{
if (Session[“FileContents”] != null)
{
downloadFrame.Attributes[“src”] = @”DownloadFile.aspx”;
}
}

In the Download.aspx page, we have this code to send the file to the browser and clear out the variables. You can see that the code that allows the browser to download it is pretty standard, setting the ContentType and calling Response.End:

protected void Page_Load(object sender, EventArgs e)
{
if (Session[“FileContents”] != null)
{
try
{
Response.ContentType = “text/plain”;
Response.AddHeader(“Content-Disposition”, “attachment;filename=CoolFileDude.dat”;
Response.ClearContent();
Response.Buffer = true;
Response.Write(Session[“FileContents”].ToString());
Session[“Contents”] = null;
Response.End();
}
}
}

Pretty it ain’t, but work it does.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: