0% found this document useful (0 votes)
27 views3 pages

Downloading Different Types of File From Server Using ASP - NET C#

This document outlines steps to download different file types from a server using ASP.NET C#. It discusses getting the project location and response, creating a method to download files by passing the file name, and creating a button event to trigger downloading a file by its name entered in a textbox. The method maps the file path, clears and appends headers to the response, sets the content type, and writes and flushes the file to download it.

Uploaded by

Lajapathy Arun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
27 views3 pages

Downloading Different Types of File From Server Using ASP - NET C#

This document outlines steps to download different file types from a server using ASP.NET C#. It discusses getting the project location and response, creating a method to download files by passing the file name, and creating a button event to trigger downloading a file by its name entered in a textbox. The method maps the file path, clears and appends headers to the response, sets the content type, and writes and flushes the file to download it.

Uploaded by

Lajapathy Arun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

Downloading Different Types of File from Server using ASP.

NET C#

In this article we are going to see How to download different types of file from the server. Follow the steps and comments above code for clear understanding. File Types like: PDF, TXT, XLS etc Design View:

Used Namespace:
using System; using System.Web;

Step 1 : This is used to get Project Location to download File inside the Current Project.
/// <summary> /// This is used to get the current Project Location. /// </summary> public static string ServerMapPath(string path) { return HttpContext.Current.Server.MapPath(path); }

Step 2: Next Step, we have to get the current response.


/// <summary> /// This is used to get Current Response. /// </summary> public static HttpResponse GetHttpResponse() { return HttpContext.Current.Response; }

Step 3: This method Helps to download any type of File within the server by passing filename in parameter.
/// <summary> /// This is used to download file from server. /// </summary> /// <param name="fileName"></param> public static void DownLoadFileFromServer(string fileName) { //This is used to get Project Location. string filePath = ServerMapPath(fileName); //This is used to get the current response. HttpResponse res = GetHttpResponse(); res.Clear(); res.AppendHeader("content-disposition", "attachment; filename=" + filePath); res.ContentType = "application/octet-stream"; res.WriteFile(filePath); res.Flush(); res.End(); }

Step 4 : Creating Event for Button to download File from server by providing filename in the textbox.
protected void Button1_Click(object sender, EventArgs e) { //string fileName = "DownloadTextFile.txt"; string fileName = FileNameTxt.Text; //This method helps to download File from Server. DownLoadFileFromServer(fileName); }

Thus I have explained how to Download File form server by specifying filename. Thanks for reading this article. Have a nice day .

You might also like