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


Recently, I've been working on a web application project where I needed to display PDF files from a local server folder. Here are the steps I followed:

1. Download the PDF file from a link.

2. Save the downloaded PDF file to a local folder. If you're working on a web application, it's necessary to store it in the server folder.

3. Display the 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.quickpickdeal.com/files/SamplePdf1_12mb_6pages.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:


Button Click Event (Button1_Click):

  • This method is executed when Button1 is clicked. Inside this method:
  • string filesave = Server.MapPath("~/DownloaddFiles/mypdffile.pdf");: This line specifies the local path where the downloaded PDF file will be saved. The Server.MapPath method translates a virtual path to a physical path on the server.
  • string pdfurl = "https://www.quickpickdeal.com/files/SamplePdf1_12mb_6pages.pdf" 
  • This line defines the URL of the PDF file that needs to be downloaded.
  • DownLoadpdfFromUrl(pdfurl, filesave);: This line calls the DownLoadpdfFromUrl method, passing the PDF URL and the local file path as arguments.
  • iframepdf.Src = "~/DownloaddFiles/mypdffile.pdf";: This line sets the source (Src) property of an iframe named iframepdf to the local path of the downloaded PDF file. This is likely used to display the PDF file on the web page.
Download PDF from URL (DownLoadpdfFromUrl method):

  • This method is responsible for downloading the PDF file from the specified URL and saving it to the local directory.
Inside the method:
  • using (var client = new System.Net.WebClient()) { ... }: This constructs a WebClient object to download the file from the URL.
  • client.DownloadFile(url, savepath);: This line downloads the file from the specified URL (url) and saves it to the local path (savepath) using the DownloadFile method of the WebClient class.
This code snippet allows the user to download a PDF file from a URL, save it locally, and then display it on the web page using an iframe.