0

I want to make a button (VIEW ONLINE) so that people can view PDF online in new tab when they click on it and those who want to download they can easily download. i have developed download but failed to make VIEW ONLINE .

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/App_Data/urg_list/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }
}
protected void DownloadFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
    Response.WriteFile(filePath);
    Response.End();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • There are two approaches here. You can rely on the browser to recognise that it's a pdf file or you can embed a pdf reader in your webpage and then display the pdf. If you don't want o be left at the whims of the browser I'd advise you try the second method. – Mohamed Najiullah Nov 16 '17 at 06:03
  • i want code that when i click on view online it auto opens the file in new tab – Wakeel danish Nov 16 '17 at 06:06
  • I think @chidambaram's answer should do the trick if you want it to open in browser. – Mohamed Najiullah Nov 16 '17 at 06:14

1 Answers1

1

For that you have to set the content disposition as inline instead of attachment. Try the below, this may work.

Response.AddHeader("content-disposition", "inline;filename=" + filename + ".pdf");

or

Response.AppendHeader("Content-Disposition", "inline; filename=" + Path.GetFileName(filePath));
Chidambaram
  • 434
  • 4
  • 14