File Downloading in ASP.NET

Posted by Zafar Ullah - zafarjcp@gmail.com | 10:52 AM | , , , , | 0 comments »


Few days back i come accross a problem of file downloading in asp.net. In my situation i have to download any type of file which is uploaded in my application.so when i upload file i save its path and content type in database.

Now when you download a file you need to change header information of page and here is the solution that works for me.


System.IO.FileInfo file = new System.IO.FileInfo(strURL);
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.ContentType = args[2]; //content type from database
Response.WriteFile(strURL);
Response.Flush();
Response.End();


By default the download dialog box will give Open,Save,Cancel buttons but if you want to disable Open/Save then add this line in your Html > Head section


<meta name="DownloadOptions" content="noopennosave" />