Geeks With Blogs
The Unstable Mind of a .Net Developer

It is a known fact that IE attempts to sniff out the mime type of files as they are downloaded.  That is, IE checks the extension of the file, the content type, etc to try to determine if there is an application on the client PC that has been configured as the default for that type of file.  So, when downloading a file with a .txt extension, it would offer to open the file with Notepad, or Excel if the file had a .csv extension.

This is fine most of the time.  But, occasionally, developers may want to force the user to download the file before it is opened, especially for large files or files with a large number of records (At least in previous versions, Excel was limited to 65k records.  Anything larger would fail.).  The problem is that because IE sniffs out the mime type, the usual trick of setting the mime type to “application/octet-stream” doesn’t work.  You could also change the name of the file to be downloaded, but that degrades the user experience because they then have to type in the correct name.

Fortunately, IE implements a poorly documented feature called DownloadOptions.  Configuring this header on a webpage tells IE to disable either the Open or Save button on the IE Download file dialog for files downloaded from the page.  The configuration syntax for the header is:

 

<meta name="DownloadOptions" content="noopen|nosave" />

 

Implementing this feature is pretty straightforward for websites developed strictly with HTML or classic ASP.  But, if the website is developed using ASP.Net v2.0 or higher, implementation can be problematic.  You see, in order to function, the DownloadOptions header MUST be configured at the top level page.  So, configuring the header does not have the expected results when set in a page that implements Master pages (You ARE using Master pages, aren’t you?).  The header must be configured in the Master page.

Now, if you plan on disabling the Open or Save button across your entire website, this isn’t an issue.  However, if you plan on only doing this for a page or two, an alternate solution is absolutely necessary.  As it turns out, that solution is fairly simple to implement.

 

BasePage

First, it is necessary to develop a base page class that implements System.Web.UI.Page.  Create boolean properties on this class called DisableOpen and DisableSave.  I have explicitly created the properties for clarity.

namespace WebApplication1
{
    public class BasePage : System.Web.UI.Page
    {
        private bool disableOpen;
        private bool disableSave;

        public bool DisableOpen
        {
            get { return disableOpen; }
            set { disableOpen = value;}
        }

        public bool DisableSave
        {
            get { return disableSave; }
            set { disableSave = value; }
        }
    }
}

MasterPage

Next, create a master page.  In the page onload event, test to see if the current page implements the base page.  If so, check to see if either disable property is set to true and configure the DownloadOptions header appropriately.  Notice that I do check to make sure that the page in question implements the BasePage class before I continue.  This would allow you to inherit from BasePage for specific set of pages.  It also protects you if future pages are developed that do not inherit from BasePage.

using System;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page is BasePage && (((BasePage)Page).DisableOpen || ((BasePage)Page).DisableSave))
            {
                // Build the HTML meta tag
                HtmlMeta meta=new HtmlMeta();
                meta.Name = "DownloadOptions";

                if (((BasePage)Page).DisableOpen)
                {
                    // Disable the open button
                    meta.Content = "noopen";
                }
                if (((BasePage)Page).DisableSave)
                {
                    // Disable the save button
                    meta.Content = "nosave";
                }

                // Add the meta tag to the page
                Page.Header.Controls.Add(meta);
            }
        }
    }
}

Implementation

At this point, the rest is very simple.  Create your web pages that use the Master page, implement the BasePage class and set the appropriate property in the page onload event.  I would like to point out that the default value for boolean properties is false, so if you do not wish to disable either button, there is nothing left to do.  If you want to disable the Open button, simply set the DisableOpen property to true.  BTW, I’ve included the download code so you can see I am doing nothing special with the mime type.

using System;

namespace WebApplication1
{
    public partial class DisableOpen : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Disable to open button
            DisableOpen = true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Type", "text/plain");
            Response.AddHeader("Content-Disposition", "attachment; filename=\"wireless.txt\"");
            Response.TransmitFile("wireless.txt");
            Response.End();
        }
    }
}

 

disableopen

 

And, there you have it.  The Open button is disabled, forcing the user to download the file and open it from their desktop.

 

Caveat

Be warned.  This solution applies only to IE users.  The DownloadOptions header is not currently part of the HTTP standard.  So, it may not be implemented in other browsers such as Google Chrome or Firefox or they may have their own (different) implementation.  Don’t you just love standards!

 

I would like to point out that the same functionality could be implemented using interfaces.  Using a BasePage class simply reduces the amount of work necessary because the properties are already defined.

 

Happy coding!

 

Ralph Wheaton
Microsoft Certified Technology Specialist
Microsoft Certified Professional Developer

Posted on Wednesday, September 2, 2009 1:47 PM ASP.Net , Internet Explorer , DownloadOptions , MasterPages | Back to top


Comments on this post: Disabling the Open button in Internet Explorer

# re: Disabling the Open button in Internet Explorer
Requesting Gravatar...
Hi,
Good one.
Could you please explain me how to implement this without MasterPage.

Thanks in advance
Left by Prasad on Nov 11, 2010 8:38 AM

# re: Disabling the Open button in Internet Explorer
Requesting Gravatar...
Para IE: Response.AddHeader("X-Download-Options", "noopen");
Left by Valeria on Jan 26, 2011 1:00 PM

# re: Disabling the Open button in Internet Explorer
Requesting Gravatar...
Hi,
Response.AddHeader("X-Download-Options", "noopen");
is to remove the Open button in IE.
Is there anyway to remove the "Save" button in Download manager?
Thanks in advance.
Left by Pwint Mar on Nov 28, 2013 7:17 PM

# re: Disabling the Open button in Internet Explorer
Requesting Gravatar...
Response.AddHeader("X-Download-Options", "noopen");
is working for me .
thn u
Left by Husnu on Jan 07, 2014 2:35 AM

Your comment:
 (will show your gravatar)


Copyright © Ralph Wheaton | Powered by: GeeksWithBlogs.net