Hi namojainashis...,
In order to dispalay base64 string as pdf in new tab use Javascript.
Check the example.
For this example i have used database to fetch the base64 string.
Once you got the base64 string you need to convert it to byte array and display the byte array as pdf in new tab.
HTML
<asp:Button Text="Display PDF" runat="server" OnClick="OnView" />
Namespaces
C#
using System.Data.SqlClient;
using System.Text;
VB.Net
Imports System.Data.SqlClient
Imports System.Text
Code
Default
C#
protected void OnView(object sender, EventArgs e)
{
string base64String = "";
string conString = "Server=.;DataBase=Test;Integrated Security=true";
string query = "SELECT Data FROM tblFiles WHERE ID = 1";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
base64String = Convert.ToBase64String((byte[])cmd.ExecuteScalar());
con.Close();
}
}
Session["Base64String"] = base64String;
string url = "PopUp.aspx";
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.open('");
sb.Append(url);
sb.Append("');");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "script", sb.ToString());
}
VB.Net
Protected Sub OnView(ByVal sender As Object, ByVal e As EventArgs)
Dim base64String As String = ""
Dim conString As String = "Server=.;DataBase=Test;Integrated Security=true"
Dim query As String = "SELECT Data FROM tblFiles WHERE ID = 1"
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
con.Open()
base64String = Convert.ToBase64String(CType(cmd.ExecuteScalar(), Byte()))
con.Close()
End Using
End Using
Session("Base64String") = base64String
Dim url As String = "PopUp.aspx"
Dim sb As StringBuilder = New StringBuilder()
sb.Append("<script type = 'text/javascript'>")
sb.Append("window.open('")
sb.Append(url)
sb.Append("');")
sb.Append("</script>")
ClientScript.RegisterStartupScript(Me.GetType(), "script", sb.ToString())
End Sub
PopUp
C#
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Base64String"] != null)
{
byte[] bytes = Convert.FromBase64String(Session["Base64String"].ToString());
Response.Clear();
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Session("Base64String") IsNot Nothing Then
Dim bytes As Byte() = Convert.FromBase64String(Session("Base64String").ToString())
Response.Clear()
Response.ContentType = "application/pdf"
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
Response.Close()
End If
End Sub
Screenshot