HTML5: Adding a Download Button to the Ribbon File Menu
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. 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…

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.
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);
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"]);
}
}
Download the sample from GitHub and test it on your own.
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- Visual Studio 2012 or better
- TX Text Control .NET Server (trial sufficient)
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
ASP.NET MVC: Implementing a Simplistic, Custom Button Bar
For some applications, the fully-featured ribbon bar might be too overloaded with features or the ribbon concept is not required in a project. Programmatically, all ribbon tabs, groups and buttons…
ASP.NET MVC: Adding Protected Sections to Documents
A SubTextPart object represents a user-defined range of text in a TX Text Control document. A SubTextPart is basically a range of text with a Name and an ID property to store additional…
ASP.NETReportingElectronic Signature
ASP.NET: Adding Electronic Signatures to Documents
An electronic signature is in many processes legally sufficient to prove an identity. According to the U.S. Federal ESIGN Act passed in 2000, an electronic signature is an: Electronic sound,…
MVC: Loading Files from the Backstage Menu
Happy New Year, everybody! In the last blog entry, we showed how to replace the file menu with an MS Word-style backstage menu. This project shows how to load documents from a partial view in the…
MVC: Replace the File Menu with a Backstage View Menu
Microsoft Word provides a very smart way to manage documents and related data such as metadata and personal information in a separate view: The backstage view. The ribbon bar contains commands for…