« Previous Next »

Thread: Setting Content-disposition to force download prompt

Last post 11-10-2009 2:11 AM by Leo Tang - MSFT. 1 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (2 items)

Sort Posts:

  • 11-05-2009, 5:09 PM

    • smithd
    • Not Ranked
    • Joined on 11-05-2009, 9:55 PM
    • Posts 1

    Setting Content-disposition to force download prompt

    I have a simple site setup with virtual directories pointing to Document directories in order to publish documents.  What I would like to do is to ensure that any of the files that are accessed, do not try to load in their native format, but instead prompt the user to open or save the file.  I have tried adding a custom HTTP response header (from the UI), but must have the syntax wrong, because it isn't working.  Even if the header did work, I can't add a custom header for every single file that is published, as they will change often and there will be many.  So I need to know how to do this dynamically as well.

     I tried adding the header using the following syntax: 

    content-disposition: attachment; filename=test.txt

    Just to be thorough, I should add that I have removed the execute permissions, in order to keep things like .exes from trying to run instead of download.

  • 11-10-2009, 2:11 AM In reply to

    Re: Setting Content-disposition to force download prompt

    Hi,

    I add the custom header via UI, it did work for me. Could you clear the client's cache, open a new IE instance, then test again?

    One option to add the custom dynamically is using manage code. Below is a code sample, it add the custom header for the welcome.png file under the default website, hope helps.

    using(ServerManager serverManager = new ServerManager()) {
    Configuration config = serverManager.GetWebConfiguration("Default Web Site");

    ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol", "welcome.png");

    ConfigurationElementCollection customHeadersCollection = httpProtocolSection.GetCollection("customHeaders");

    ConfigurationElement addElement = customHeadersCollection.CreateElement("add");
    addElement["name"] = @"content-disposition";
    addElement["value"] = @"attachment";
    customHeadersCollection.Add(addElement);

    serverManager.CommitChanges();
    }
    Leo Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (2 items)
Microsoft Communities