In a recent post, we explained how to open a local document by uploading it to the server. This sample project shows how to add a Download button to the ribbon file menu that downloads the document as an Adobe PDF file.

HTML5: Adding a download button to the ribbon file menu

As in other samples, a hidden button is used that will be triggered inside an AJAX UpdatePanel to keep up the existing WebSocket connection. In the event handler, the document is saved using the SaveText method and temporary stored in a session variable. Finally, a client-side Javascript is injected to the DOM which creates a hidden IFRAME element that calls the same ASPX page with a separate HTTP context. This way, a post back is not required and the Web.TextControl won't be disconnected from the server.

// save the document as PDF in a session variable
byte[] data;
TextControl1.SaveText(out data,
TXTextControl.Web.BinaryStreamType.AdobePDF);
Session["document"] = data;
// create a new IFRAME and call this ASPX with QueryString
// to load document from session data
ScriptManager.RegisterClientScriptBlock(
this.Page,
this.Page.GetType(),
"alert",
"var iframe = document.createElement('iframe');" +
"iframe.src = 'index.aspx?download=1';" +
"iframe.style.display = 'none';" +
"document.body.appendChild(iframe);",
true);
view raw index.aspx.cs hosted with ❤ by GitHub

The dynamically added Javascript calls the index.aspx page again with the query string document=1. This jumps directly into the Page_Load event where the stored document from the session variable is returned to the hidden IFRAME that will open the document in the browser.

protected void Page_Load(object sender, EventArgs e)
{
// execute only when loaded from IFRAME
if (Request.QueryString["download"] == "1")
{
if (Session["document"] == null)
return;
// return the stored document
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition",
"attachment; filename=results.pdf");
Response.BinaryWrite((byte[])Session["document"]);
}
}
view raw index.aspx.cs hosted with ❤ by GitHub

Download the sample from GitHub and test it on your own.