View PDF in browser by Selecting FileUpload using C# in ASP.Net

Last Reply one year ago By Indresh

Posted one year ago

 

I want to view in the current page itself simply to enable the user to verify what they are going to upload .Also i want it to be done soon after selecting the pdf from fileupload to avoud the burden of clicking a button......plz help

Posted one year ago Modified on one year ago

Hi pankaj123,

I have created sample code by refering the below article which full-fill your requirement.

Open (View) PDF Files on Browser in ASP.Net using C# and VB.Net

HTML

<form id="form1" runat="server">
<div>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('[Id*=FileUpload1]').change(function () {
                $('[Id*=btnView]').click();
            });
        });
    </script>
</div>
<div>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <asp:Literal ID="ltEmbed" runat="server" />
    <br />
    <asp:Button ID="btnView" Text="Submit" OnClick="OnCheck" Style="display: none" runat="server" />
</div>
</form>

C#

protected void OnCheck(object sender, EventArgs e)
{
    ltEmbed.Visible = true;
    Viewme();
}
private void Viewme()
{
    using (Stream fs = FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            Session["ByteArray"] = bytes;
            Session["FileName"] = FileUpload1.PostedFile.FileName;
            string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string contentType = FileUpload1.PostedFile.ContentType;
            string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
            embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
            embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
            embed += "</object>";
            ltEmbed.Text = string.Format(embed, ResolveUrl("~/PdfViewer_Handler.ashx"));
        }
    }
}

PdfView_Handler.ashx

<%@ WebHandler Language="C#" Class="PdfViewer_Handler" %>

using System;
using System.Web;

public class PdfViewer_Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        byte[] bytes = context.Session["ByteArray"] as byte[];
        string fileName = context.Session["FileName"].ToString();
        context.Response.Buffer = true;
        context.Response.Charset = "";
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.ContentType = "application/pdf";
        context.Response.BinaryWrite(bytes);
        context.Response.Flush();
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Screenshot