I display a selected set of documents in the document library within a SPGridView in a webpart. The documents should be downloadable and i use LinkButton for this purpose. A method is used on the LinkButton is click event to do this. The method has the below code which runs within Elevated privileges.
SPFile aa = documentsLibrary.GetItemByUniqueId(fileStr).File;
using (MemoryStream ms = new MemoryStream(aa.OpenBinary()))
{
Response.Clear();
// Clear the content of the response
Response.ClearContent();
Response.ClearHeaders();
// Buffer response so that page is sent after processing is complete.
Response.BufferOutput = true;
// Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + aa.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", aa.Length.ToString());
// Set the ContentType - octet stream helps handle all content types generically
Response.ContentType = "application/octet-stream";
ms.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
}
The method triggers on LinkButton's Click event. On the very first click on any LinkButton in the Grid, the document gets downloaded. But after that, any other click on other rows LinkButton doesn't work. Nothing happens.
So, the problem is that only the first click works, any other click on the other row items in the grid does not download other documents. After page refreshes only the first download works .
Added the Response.redirect("pageUrl");
at the bottom after closing. Then the document does not get downloaded at all. Should be due to the fact response.close() already closes it.
Is there a way to refresh the page once a download is done ?