Home > ASP.NET MVC > Show Save As Dialog With Content-Disposition Response Header

Show Save As Dialog With Content-Disposition Response Header

In this post I will talk about how you can force the browser to show a Save As dialog with a predefined filename. For the purpose of my example I will build on my previous post.


This might be really useful in some type of applications – a file manager, email client application like Gmail (downloading file attachments) and exporting data into different formats. You could rely on the browser to show Save As dialog but that might not be the case. It will prompt you to save a file if it can’t render its contents (the content type is unknown) but it will set the filename to the url it was downloaded from (you do not have a choice to give it a more proper name). Luckily there is a special response header part of the HTTP Specification that accomplishes that.

Content-Disposition Header

The Content-Disposition header is optional and gives you the ability to explicitly specify the file name of the requested resource and force the showing of a Save As dialog. The file resource could be specified as inline (default one) or attachment. If omitted the browser tries to render it as if it is specified as inline. In our case we need to set it as an attachment so that we can force the Show As dialog to pop up:

Content-Disposition: attachment

With the filename parameter we notify what the file name of the requested resource should be. We can give it any meaningful name we want. If the requested relative url is the following /MySite/Export and the filename parameter is missing then the file name that the browser would suggest will be Export. This is in no way user-friendly. Here is how we can set the file name using the Content-Disposition header:

Content-Disposition: attachment; filename=ExportedUser_06272010.xml

Now the Save As dialog would suggest the file be saved as ExportedUser_06272010.xml. You can implement any kind of naming convention for your application and make it more user-friendly.

Server side modifications

Since we already know how to accomplish everything we can change the action method responsible for exporting users to xml from my previous post:

public ActionResult Export()
    User user = HttpContext.Cache["user"] as User;
    if (user == null)
        return View("Index");

    HttpContext.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}_{1}.xml", user.FirstName, user.LastName));
    return View("User", user);

The important part is where we add the Content-Disposition header to the response using the AddHeader method on the HttpContext’s Response property. If we start the application and click on the Export button the Save As dialog pops up:

Content-Disposition Header

Content-Disposition Header


In some scenarios you need to have control over the browser and force it to show the Save As dialog and explicitly set the file name of the resource. This is accomplished with the Content-Disposition response header. I have attached the updated application so you can play around with it.

Categories: ASP.NET MVC
  1. August 4, 2013 at 9:27 pm

    Hello i am kavin, its my first occasion to commenting
    anywhere, when i read this article i thought i could also create comment due to
    this brilliant piece of writing.

  2. Johnf944
    May 2, 2014 at 10:36 pm

    Thank you for every other informative blog. The place else may just I get that type of info written in such a perfect means? I’ve a project that I’m just now working on, and I have been at the glance out for such information. gdebcddebkfe

  3. October 6, 2014 at 11:43 pm

    Μόλις προστέθηκε στα bookmark
    μου! Μ’ αρέσει πολύ το blog. Συνέχισε την
    καλή δουλειά σου.

  4. October 7, 2014 at 12:25 am

    Αυτό είναι ακριβώς που χρειαζόμουνα.
    Και το βρήκα με μια απλή αναζήτηση στη λέξη στο Google.

  5. October 8, 2014 at 4:17 pm

    Περιπλανιέμαι στο ιντερνέτ σήμερα για περισσότερες από 4 ώρες και δεν βρήκα πολλά άρθρα που να είναι εξίσου ενδιαφέροντα με το
    δικό σας. Πιστεύω ότι αν όλοι οι webmasters των sites έφτιαχναν τέτοιο ποιοτικό περιεχόμενο το ιντερνετ,
    θα ήταν πολύ καλύτερο για όλους.

  6. Suhail
    December 29, 2016 at 12:25 pm

    Hello I am Suhail. I am able to download the file, but the save dialog is not appearing. I am using chrome. Any idea?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: