[Solved]-Download pdf file from link and save in local file folder in Asp .Net

[Solved]-Download pdf file from link and save in local file folder in Asp .Net

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:

Ashok Patel

I'm an electronic engineer working in a multinational company,having good experience on Electronics and electrical engineers design and oversee production of electronic equipment such as radios, televisions, computers, washing machines and telecommunication systems.I like to do RND and Research.I also have hands on experience graphic design software and in web designing having great command on ASP.NET, HTML5, JavaScript, T-SQL, JQuery.

Add comment

Donate for Corona Victim

Corona Virus Relief Fund

Your Header Sidebar area is currently empty. Hurry up and add some widgets.