For a customer I was creating a project where they needed to click a button and download a file.
This is basic ASP.net stuff, BUT in SharePoint there are some issues with this.
If you use this code, which is the code to push a file towards the client browser for download:
Response.Clear();
Response.Buffer = true;
Response.BufferOutput = true;
Response.ContentType = "application/x-download";
Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
Response.CacheControl = "public";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.Flush();
This code will work once,BUT next time you click the button it will not work.
Note: I’m using a byte array to publish, reason is that I created in this case an XML at runtime, this will be handled in an other blog
Solution:
We need to add Javascript to the button !!!
protected override void OnLoad(EventArgs e)
{
public string ScriptRefresh =@"function Refresh()
{
window.setTimeout(
function()
{
_spFormOnSubmitCalled = false;
}, 10);
} "; //This string represents the javascript fore this issue, it could be any kind of javascript.
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"Refresh",ScriptRefresh, true); //Here we add the Javascript to the page ,ScriptRefresh = javascript, you could type it directly here if you want
btnDownLoadXML.OnClientClick = "Refresh()"; //Here we add the javascript to the button
}
Thats actually all you need to do.
What you are doing behind the scene is tell SharePoint that the page is not submitted yet.
Hope it helps
Published: 12/22/2011 | 0 Comments | 0 Links to this post