Force a Save AS dialog box inside a browser and IIS 6 Compression

by Steve Schofield

One of the things I enjoy working on Internet technologies is the variety of challenging situations that come up.  I worked on something where a person was trying to force a download dialog box to appear in a users Internet browser.  There are tons of examples on the Internet showing how to do this, if not just Google on ‘Force download dialog box’.  I’m assuming you already understand how to configure IIS compression or the server was already configured.  If you are not familiar with IIS compression,  I reading this article on setting up IIS 6 compression. http://weblogs.asp.net/owscott/archive/2004/01/12/57916.aspx.  I used two commands that are listed below to disable IIS compression my ‘test’ virtual directory.

The issue came up where the ‘document name’ was appear in the ‘Save As’ browser window instead of the actual file name they wanted to present to the user.  This articles main goal is to describe what I did to locate the issue and how to correct it.  When trying to force a dialog box such as this listed below, notice the MyPDF.PDF is displayed.  This is what it is supposed to look like. 

Here are the headers from it working.

HTTP/1.1 200 OK
Cache-Control: private
Date: Sat, 24 Dec 2022 06:21:29 GMT
Content-Type: application/pdf; charset=utf-8
Server: Microsoft-IIS/6.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
content-disposition: attachment; filename=MyPDF.PDF
Content-Encoding: gzip
Vary: Accept-Encoding
Transfer-Encoding: chunked

What I ran into and was kind of baffled why it wasn’t working.  Notice in the picture, the ‘Name:’ property has ‘fd_aspx’ instead of ‘myPDF.PDF’ like the picture above.   If you click on the Save button, you’ll get an error.

Here are the HTML headers using ieHTTPHeaders utility.

HTTP/1.1 200 OK
Cache-Control: no-cache
Date: Sat, 24 Dec 2022 06:16:48 GMT
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: application/pdf; charset=utf-8
Expires: -1
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.2023
content-disposition: attachment; filename=myPDF.pdf
Content-Encoding: gzip
Vary: Accept-Encoding

After I turned off compression on the ‘test’ virtual directory, the ‘Save AS’ dialog worked.  Using compression can help save money if you are charged for bandwidth, however it can cause certain issues like this above.  The next time you are having a problem, remember IIS compression and you can potentially save yourself hours of troubleshooting.. 

—————-

Side notes:

Some other facts I discovered while working through this FireFox didn’t have any issues with compression and rendering the correct filename.  After Googling around, I found a blog by Scott Hanselman that described his adventure trying to force a Save AS dialog box and how IE had some issues trying to get this to work.  Its a good read. Makes me wonder why IE has issues for items like this. 

—————-

Another "cool tool" is called ieHTTPHeaders (http://www.blunck.info/iehttpheaders.html) that helped troubleshoot.  ieHTTPHeaders is an Explorer Bar for Microsoft Internet Explorer that will display the HTTP Headers sent and received by Internet Explorer as you surf the web. It can be useful in debugging various web-development problems related to cookies, caching, etc.

—————-

Sample code used to force the download.  Original article in C# ( http://www.eggheadcafe.com/articles/20011006.asp )

<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<HTML>
<HEAD>
<script runat="server" ID=Script1>
    Sub Page_Load(sender as object, e as System.EventArgs)
        If Page.IsPostBack = True then

            Dim strFS As String = Server.MapPath(".")
            Dim MyFileStream As FileStream = New FileStream(strFS & "small.pdf",FileMode.Open)
            Dim FileSize As Long
            FileSize = MyFileStream.Length

            Dim Buffer(CInt(FileSize)) As Byte
            MyFileStream.Read(Buffer, 0, CType(MyFileStream.Length, Integer))
            MyFileStream.Close()
           

            Response.ContentType="application/pdf"
            Response.AddHeader("content-disposition","attachment; filename=MyPDF.PDF")
            Response.BinaryWrite(Buffer)
        End if
    End Sub
</script>
</HEAD>
<body>
<form runat="server">
<asp:button id="link1" Text = "get PDF" runat="server" />
</form>
</body>
</HTML>

Enjoy!

Steve