0

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 ?

1

Thanks for the responses,

Actually I came across a remedy in a similar article

Referring to tha post answer, in my code I invoked the below javascript OnClientClick event of the LinkButton.

function setFormSubmitToFalse() {
    setTimeout(function () { _spFormOnSubmitCalled = false; }, 3000);
    return true;
}
0

Most likely you are data-binding the grid in the Page_Load event. The Page_Load event happens on every postback - every button click, or any event that triggers the postback.

If this is the case, there are two possible options:

Move your data binding code to Page_Init

Put your data-binding in Page_Load inside an if(!Page.IsPostback) block.

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.