Monday, September 8, 2008

How to prompt for Download for a known MIME type

Sometimes you may require a link to download some files like jpg, gif, html etc. But these are known mime types and if you just give a 'href' link it opens in the browser itself. To prompt the user for 'Save as' you need to use the following steps,

Response.ContentType = "application/x-unknown";
string fileName = "file.jpg";
string filePath = "d:\\" + fileName;
Response.AddHeader("Content-Disposition","attachment; filename=" + fileName );
FileStream fs = File.OpenRead(filePath);
BinaryReader br = new BinaryReader(fs);
Response.BinaryWrite(br.ReadBytes(Convert.ToInt32(fs.Length)));
Response.End();

This is code in c#. Similar way you can use this in any other scripting language.

Tags: Response.ContentType, Content-Disposition, ASP.NET, C#, Download a file using c# and ASP.NET

Add To Google BookmarksStumble ThisFav This With TechnoratiAdd To Del.icio.usDigg ThisAdd To RedditTwit ThisAdd To FacebookAdd To Yahoo

No comments: