SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In one of my webpart I have a feature to generate a file dynamically depending on what the users choosed in the view.

On my OnClick event handler, I have this code :

protected void reportClick(object sender, EventArgs e)
    MemoryStream ms = GenerateFile();

    ms.Seek(0, SeekOrigin.Begin);

    byte[] byteArray = ms.ToArray();


    Context.Response.AddHeader("Content-Disposition", "attachment; filename=report.docx");
    Context.Response.AddHeader("Content-Length", byteArray.Length.ToString());
    Context.Response.ContentType = "application/octet-stream";

It works as the file is proposed to download to the user. The problem is that after the download, the page is blocked: the buttons are still clickable but there are no actions done on the click. I have to refresh the page to enable the actions again.

Is there a way to download a file without blocking the page ?

share|improve this question
up vote 2 down vote accepted

In your webpart code you can do this by rendering a hyperlink to a SharePoint application page (under the _layouts folder), rather than a button. The application page should contain the code to dynamically generate your file.

share|improve this answer
Another option will be using custom HTTP handler, as this is the mechanism dedicated to handle such a scenario in ASP.NET. – Maksymilian Mulawa Mar 9 '12 at 10:11
If I use another page, how do I pass information to this page from the first one to know what to generate ? Can I pass a MemoryStream to the other page or a List ? – Baptiste Wicht Mar 9 '12 at 11:32
How about using query string parameters? – Howard Mar 9 '12 at 11:50
Querystring would be best – Fox Mar 9 '12 at 12:09
The problem with QueryString is that I have to give it a list of IDs so I have to parse it to pass it to the URL and then parse it again to get the ID. But that's a good start, thanks. – Baptiste Wicht Mar 12 '12 at 9:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.