Click here to Skip to main content
13,293,369 members (47,511 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 7 Mar 2012

Download all types of files from server using ASP.NET

, 8 Mar 2012
Rate this:
Please Sign up or sign in to vote.
Download any type of file from server using with javascript


Download any type of file from server using and javascript. Front end User not able see the files downloading from which server path.


1. Query string not used.

2. User not able to find the path of the file. 

3. User not able to download other files.

Using the code

I used hyperlink control in main.aspx page to download the file, while click the hyperlink page will redirect to download.aspx and it will download the file automatically.

Main.aspx - hyperlink control and javascript

<script type="text/javascript" language="javascript" > 
function InitializeRequest(path) {
	// call server side method 

	// Create an IFRAME.
	var iframe = document.createElement("iframe"); 
	iframe.src = "Downloads.aspx";

	// This makes the IFRAME invisible to the user. = "none";

	// Add the IFRAME to the page. This will trigger
	// a request to GenerateFile now.
Note: If you are using PageMethods in javascript then should be enable the EnablePageMethods is true in asp:ScriptManager control
Ex: <asp:ScriptManager ID="sc1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:HyperLink NavigateUrl="javascript:void(0)" ID="lnkDownloadHSPressPack" runat="server" >Download File</asp:HyperLink> 
Main.aspx.cs - Call javascript function in hyperlink onclick event

protected void Page_Load(object sender, EventArgs e)
	strDownloadPdfLink = "/Documents/PressPack.pdf";
	lnkDownloadHSPressPack.Attributes.Add("onclick", "InitializeRequest('" + strDownloadPdfLink + "');");

Web Method for assign the file path to session variable.  This web method call from InitializeRequest javascript function.
/// <summary>
/// WebMethod - Get the PDF path from javascript and assign to session variable
/// </summary>
/// <param name="strpath"></param> 
public static string SetDownloadPath(string strpath)
	Page objp = new Page();
	objp.Session["strDwnPath"] = strpath; 
	return strpath;

Downloads.aspx - Download the files based on session, this page redirect from InitializeRequest javascript function (Main.aspx)

after downloaded the file again redirect to main.aspx page

protected void Page_Load(object sender, EventArgs e)
string filePath = string.Empty;
	bool blnFileDownload = false;
	if (!string.IsNullOrEmpty(Session["strDwnPath"].ToString()))
		filePath = Session["strDwnPath"].ToString();
		Session["strDwnPath"] = "";
	if (filePath == "") return;
	// Clear the content of the response
	// Buffer response so that page is sent
	// after processing is complete.
	Response.BufferOutput = true;
	// Add the file name and attachment,
	// which will force the open/cance/save dialog to show, to the header
	string fileName = filePath;
	if (filePath.Contains("/Documents/"))
		fileName = filePath.Split('/')[2];
		blnFileDownload = true;
		blnFileDownload = false;
	if (blnFileDownload == true && (File.Exists(Request.PhysicalApplicationPath + filePath) || File.Exists(Server.MapPath(filePath))))
		Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
		Response.ContentType = "application/octet-stream";
		if (Request.UrlReferrer != null)
			Type csType = GetType();
			string jsScript = "alert('File Not Found');";
			ScriptManager.RegisterClientScriptBlock(Page, csType, "popup", jsScript, true);
catch (Exception ex)
	string errorMsg = ex.Message;



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ramanujam Shankar
Web Developer Mahindra Logisoft Business Solution Limited, Chenn
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHi Pin
Rahim Lotfi19-Feb-14 11:02
memberRahim Lotfi19-Feb-14 11:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 8 Mar 2012
Article Copyright 2012 by Ramanujam Shankar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid