Recently I’m working on the Web Application project. In which i have to display PDF file in from our local server folder. For that, I have followed the below step

  1. Download PDF file from a Link.
  2. Downloaded PDF file in the local folder if you working on a web application then you need to save it in the server folder.
  3. Display PDF files from the local folder.

Html Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Click To Download Pdf" OnClick="Button1_Click" />
        <br />
         <br />
        <iframe runat="server" id="iframepdf" height="600" width="800" src=""> </iframe>
    </div>
    </form>
</body>
</html>

 

Backend Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string filesave = Server.MapPath("~/DownloaddFiles/mypdffile.pdf");
        string pdfurl = "https://www.learningcontainer.com/wp-content/uploads/2019/09/sample-pdf-file.pdf";
        DownLoadpdfFromUrl(pdfurl,filesave);
        iframepdf.Src = "~/DownloaddFiles/mypdffile.pdf";
    }
    public void DownLoadpdfFromUrl(string url, string savepath)
    {
        using (var client = new System.Net.WebClient())
        {
            client.DownloadFile(url, savepath);
        }
    }
}

Dowload Source Code

Result:

The post [Solved]-Download pdf file from link and save in local file folder in Asp .Net appeared first on Software Development | Programming Tutorials.



Read More Articles