.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Downloading files

Posted By:Athira Sreejith       Posted Date: February 05, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Below is the sample code to Doliad file from the server using asp.net and C#.

using System.IO;

using System.DirectoryServices;

using System.Data.OleDb;

private void ImgDwld_Click(object sender, System.Web.UI.ImageClickEventArgs e)


------Getting a QID-------------

int t=int.Parse(temp.Text.ToString());

string FileToOpen="";



----By using that UID we are getting the DownLoad File Name for the corresponding Quesition--



if (d!="")


---"ownloadfiles"is the folder name where the files are stored-------------

---- By using the Server.MapPath we are getting the path for "ownloadfiles" folder--

string strPath= Server.MapPath("downloadfiles") + "/" + d;

---------we are passing the path of the file ---------------------




public void DownloadFilesInfo(string strPath)


DirectoryInfo fi = new DirectoryInfo(strPath);

------------we are getting the corresponding file name separately from the path---

string fname = Path.GetFileName(strPath);

---------Here we are passing the Full Path & getting the content type---------------------

string contentType = GetContentType(strPath); // application/WinWord

Response.ContentType = contentType;

Response.AppendHeader("Content-Disposition", "attachment; filename=" + fname);


string strStatus1 = Response.ContentType.Trim();

---------------- Response.Flush() is used to open dialog box for the DownLoad Window----

Model Screen shot for the Response.Flush()



string strStatus2 = Response.CacheControl.Trim();

strStatus2 = strStatus2.ToString();

string strStatus3 = Response.Status.ToString();

strStatus3 = strStatus3.ToString();


public string GetContentType(string FileName)


----------Here we are Getting the EXTENSION of the corresponding File---------------

string Ext=System.IO.Path.GetExtension(FileName);

---------- Open the File type Registry Key --------------------------------------

Microsoft.Win32.RegistryKey reg1 = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(Ext);

return reg1.GetValue("Content Type").ToString();

------------Here we are passing the Content Type as "text/plain" -------------------------







Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend