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

Last Reply on Feb 17, 2017 02:43 AM By Indresh

Posted on Feb 13, 2017 02:20 AM

 

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

Results 1 - 5 of 15 123
Posted on Feb 13, 2017 04:50 AM

Hi pankaj123,

Will you please explain in more detail about your requirement?


Posted on Feb 13, 2017 05:38 AM
Indresh says:

Hi pankaj123,

Will you please explain in more detail about your requirement?

 i have a file upload control and i want to see the pdf selected on the same page before moving to save it in the database.iwant it to be done on the selected index change of the fileupload i.e i dont want to use a button click for this.

i tried with a function call on button click event the code is

 

 protected void Button1_Click(object sender, EventArgs e)
    {
        ltEmbed.Visible = true;
        Viewme();
    }

 protected void Viewme()
    {
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string contentType = FileUpload1.PostedFile.ContentType;
        using (Stream fs = FileUpload1.PostedFile.InputStream)
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                byte[] bytes = br.ReadBytes((Int32)fs.Length);
            }
        }

    

        string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
        embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">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("~/FileCS1.ashx?Id="), filename);
    }

code of the FileCS1.ashx is as below

 

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

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public class FileCS : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {

        string fileName = (context.Request.QueryString["Id"]);


        context.Response.Buffer = true;
        context.Response.Charset = "";
        if (context.Request.QueryString["download"] == "1")
        {
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
        }
        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;
        }
    }
    
   
}

here i am not able to transfer the binary data stored in the variable bytes to the ashx file.

 

however my requirement is to simply see the file selected from the fileupload control but have not been able to do it so far.also i want to avoid the button click.


Posted on Feb 13, 2017 08:57 AM Modified on on Feb 17, 2017 02:43 AM

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

 


Posted on Feb 13, 2017 11:18 AM
Indresh says:

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()
{
    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("Files/" + filename));
}

Screenshot

 it works but only when i put my pdf in a folder named Files in the main folder......though it worked exactly in the way wanted.......but i don't want to select from the folder .i want to select from the client system. i think i m close to the solution and something needs to be done in the code below so that actual path from the client system is selected......

ltEmbed.Text = string.Format(embed, ResolveUrl("Files/" + filename));

 


Posted on Feb 13, 2017 11:54 PM