Prevent the rtf, pdf and docx file download and convert the exported content to a string
Environment
Product | RadEditor for ASP.NET AJAX |
Description
By design, the RadEditor ExportToPdf, ExportToRtf and ExportToDocx methods automatically convert and download the exported HTML content on the client as a file. See the solution below to learn how to obtain the converted content as a string on the server-side so that you can, for example, save it in another location as a physical file or in your database.
Solution
Here is an example of how to save the ExportOutput to a file on the server and prevent it from being sent to the client:
<telerik:RadEditor RenderMode="Lightweight" runat="server" ID="RadEditor1" OnExportContent="RadEditor1_ExportContent" ContentFilters="DefaultFilters, PdfExportFilter">
</telerik:RadEditor>
<asp:Button runat="server" ID="Button1" Text="Export to PDF" OnClick="Button1_Click" />
using System;
using System.IO;
using Telerik.Web.UI;
public partial class DefaultCS : System.Web.UI.Page
{
protected void RadEditor1_ExportContent(object sender, EditorExportingArgs e)
{
string url = String.Format("~/{0}.pdf", RadEditor1.ExportSettings.FileName);
string path = Server.MapPath(url);
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream fs = File.Create(path))
{
Byte[] info = System.Text.Encoding.Default.GetBytes(e.ExportOutput);
fs.Write(info, 0, info.Length);
}
e.Cancel = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
RadEditor1.ExportToPdf();
}
}
Imports System.IO
Imports Telerik.Web.UI
Partial Class DefaultVB
Inherits System.Web.UI.Page
Protected Sub RadEditor1_ExportContent(sender As Object, e As EditorExportingArgs)
Dim url As String = String.Format("~/{0}.pdf", RadEditor1.ExportSettings.FileName)
Dim path As String = Server.MapPath(url)
If File.Exists(path) Then
File.Delete(path)
End If
Using fs As FileStream = File.Create(path)
Dim info As [Byte]() = System.Text.Encoding.[Default].GetBytes(e.ExportOutput)
fs.Write(info, 0, info.Length)
End Using
e.Cancel = True
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
RadEditor1.ExportToPdf()
End Sub
End Class