views:

595

answers:

2

We've got a page in our asp.net web system that uses response.redirect to redirect the user directly to an excel file so it will download "automatically" without the user having to to a right click / save as.

This works great - except for files over about 100k in IE7. Other browsers just download the large file fine, and IE works fine under that threshold, but at somewhere about 200k, IE just starts handing out "page cannot be displayed" errors.

Obviously, we'd like the user to be able to download in IE as well - any ideas? Is there some kind of download size threshold thing I can override?

+1  A: 

I prefer a different method for sending files. It works for me with all kinds of different types and sizes of files.

Instead of using Response.Redirect, allow the link to the file to do a postback where you modify the response, like so:

Public Shared Sub SendFileToBrowser(ByRef response As HttpResponse, ByVal filepath As String, Optional ByVal filename As String = "", Optional ByVal contentType As String = "", Optional ByVal disposition As String = "", Optional ByVal contentLength As String = "")
    Dim ext As String = filepath.Substring(filepath.Length - 3, 3)

    If String.IsNullOrEmpty(contentType) Then
        If ext = "pdf" Then
            contentType = "application/pdf"
        Else
            contentType = "application/file"
        End If
    End If

    If String.IsNullOrEmpty(disposition) Then
        disposition = "attachment"
    End If

    If String.IsNullOrEmpty(filename) Then
        ''//Test for relative url path
        Dim fileparts As String() = filepath.Split("/")
        If fileparts.Length > 1 Then
            filename = fileparts(fileparts.Length - 1)
        Else
            ''//Test for absolute file path
            Dim fileparts2 As String() = filepath.Split("\")     ''//" SO: Fix syntax highlighting
            If fileparts2.Length > 1 Then
                filename = fileparts2(fileparts2.Length - 1)
            Else
                ''//Just give it a temp name
                filename = "temp." & ext
            End If
        End If
    End If

    response.Clear()

    response.AddHeader("content-disposition", disposition & ";filename=" & filename)
    If Not String.IsNullOrEmpty(contentLength) Then
        response.AddHeader("Content-Length", contentLength)
    End If

    response.ContentType = contentType

    response.Cache.SetCacheability(HttpCacheability.Public)        

    response.TransmitFile(filepath)
    response.End()
End Sub

Note: Using "''//" for comments so that the syntax highlighter works properly. This still compiles properly as well.

This works for us in IE6 and IE7.

EndangeredMassa
Beautiful! Exactly what I needed. Thanks!
Electrons_Ahoy
Glad to help! Let me know if you run into any issues with this method.
EndangeredMassa
+1  A: 

You can make a simple ashx wrapper around this file and force IE to download this file by using http header: "Content-disposition: attachment; filename=fname.xls"

How To Raise a "File Download" Dialog Box for a Known MIME Type

maxnk