How to force a file to download in ASP.NET

There are lots of file type which can be handled by IE (or other browsers Firefox, Opera etc.) like html, xml, jpeg, gif, swf, pdf, doc etc. In case the browser can not handle the a particular file type it will ask you to open or save that file. Sometime you may want to force the user to download a particular file even though browser can open that file type because of any of your business need. This can be easily achieved by adding the content-disposition header in the response of your page.

Take below example. Here we are writing a gif file in response and forcing the user to download this file.

protected void Page_Load(object sender, EventArgs e)
  Response.AddHeader("content-disposition", "attachment;filename=logo_large.gif");
  Response.ContentType = "image/GIF";

If you see above code then you can notice we can also specify the file name. This technique will work for other file types too.

18 comment(S)

Mark Laycock on Jul 27, 2008 02:51 AM

Interesting but it doesn't indicate where the code should be inserted (between which tags, etc) - as a newbie I'm trying to get people to download PDF files from web pages (but not open in the browser) but not having any success in coding this.

Rajasekhar on Nov 11, 2008 06:17 AM

It's working, but if Download file formate is Html. I don't want to open that page is same window where I made the button click for download. I want open in different window what are changed I need to chage for above code

Ravi on Mar 7, 2011 06:55 AM

But this is not work properly

Life Is Delightful on Apr 11, 2011 09:54 PM

This code worked for me. But I want to know what all content types are available...

mytrash on Apr 28, 2011 06:31 AM

Nice piece of code, on this post the VB.NET version that let's you download all kind of files:

mitu on Dec 31, 2011 07:22 AM

it works for me. great post. keep it up.
thanks for the post.

mitu on Dec 31, 2011 07:22 AM

it works for me. great post. keep it up.
thanks for the post.

Umer on Feb 14, 2012 10:26 PM

It works fine when i place this code in Page_Load but when I put the same code in some click event (lets say on some button click than nothing shows up )

can anyone help me plz

Thanks in advance

Andrea on Apr 12, 2012 03:07 AM

The downloading of file works but my page didn't visualize as adding a new header (for the file) would delete the http header of the page instead to append the new header.
How can I both load the page and downolad the file?


abed on Jun 8, 2013 05:02 AM

damar on Jan 21, 2014 12:33 AM

hi.. i want to know how the code download the image file when there is not data in that file ? I mean on code above there is just initiate file name and the path where the file will be put. There is no data in the "logo_large.gif"

Bhavik on Mar 12, 2014 02:42 AM

Amazing... Really working.. Can i use this method for json call ?

Dosik on Jun 12, 2014 12:27 PM

Hi everybdoy
This code not work in my browser
I wanna to open a PDF file in browser not download. How I can do that ?

dfgh on Jun 13, 2014 05:32 AM


Mahes on Jul 8, 2015 12:52 PM

Simple with Best ..Thnq.....:)

sds on May 26, 2016 11:35 PM


Leave a comment