DownLoad Pdf file in Asp.net 2.0

1) Create the Simple Web Application.

2) Now add the new folder (FolderName : PDFFile).

2.1) right click on the project and add folder.

3) put the one pdf file in to folder ( name :1.pdf).

4) in the web page put the control

4.1)put the button control (name : btnPdfFiledownload)

(note: Here we use hardcoded thing (means use button control and path)

if you want to change then you do it.)

5) on the button click (means btnPdfFiledownload) you write the below code.

protected void btnPdfFiledownload_Click(object sender, EventArgs e)

{

//get the physical path of your drive + append the folder and you file name with this

//example my folder is on the same level of this (foldername ->PDFFile)

//file name is 1.pdf

string strMapPath = Server.MapPath(Request.Url.AbsolutePath.Substring(0, Request.Url.AbsolutePath.LastIndexOf(“//”) + 1)) + @”\PDFFile\1.pdf”;

//enter the content type (describe the pdf)

Response.ContentType = “application/pdf”;

//in the filename you can give whatever you want

//when download the file at that save this file with this filename

//Adds an HTTP header to the output stream.

Response.AppendHeader(“Content-Disposition”, “attachment; filename=applicationfile.pdf”);

//TransmitFile do –> Writes the specified file directly to an HTTP response output stream without buffering it in memory

Response.TransmitFile(strMapPath);

//End Response

Response.End();

}

Thank you.

Leave a Reply

Discover more from AI Infrastructure Architect & Enterprise Solution Architect

Subscribe now to keep reading and get access to the full archive.

Continue reading