2

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();

    ms.Flush();
    ms.Close();

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

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 ?

2

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.

5
  • Another option will be using custom HTTP handler, as this is the mechanism dedicated to handle such a scenario in ASP.NET. 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 ? Mar 9 '12 at 11:32
  • 1
    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. Mar 12 '12 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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