Link to home
Start Free TrialLog in
Avatar of geobaldi
geobaldiFlag for United States of America

asked on

Filename changes when downloading file with ASP.NET and IIS

We have ASP.NET code that downloads files of different types to the client.  On several of our servers, the file properly downloads with the correct name and extension (e.g. "helloworld.doc").  However, on another server the file is always renamed to the ASPX filename with .html extension.  Thus, when the user clicks download on the web browser they see the File Download dialog with name as "aspfilename.html" and type as "HTML document" instead of "helloworld.doc" and "MS Word" respectively.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim att As New Attachment
        Dim physicalPath As String
        Dim fileName As String
 
        If Me.AttachmentId > 0 Then
            ' If an attachment id is provided then use it to look up the file information.
            att = Attachment.GetAttachmentById(Me.AttachmentId)
            physicalPath = att.PhysicalPath
            fileName = att.FileName
        Else
            ' No attachment id provided so use the file information from the parameters.
            physicalPath = Me.PhysicalPath
            fileName = Me.FileName
        End If
 
        Response.AddHeader("Content-Disposition", "attachment;filename=""" + fileName + """")
        Response.TransmitFile(physicalPath)
        Response.End()
    End Sub

Open in new window

Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

AddHeader needs the Content-filetype as well.
Add this before Response.AddHeader:
Response.Clear()
Add this too before the Response.End();

Response.AppendHeader("Pragma", "no-cache");
I have done this several times, and this is all you need.  Note the [attachment; filename=file.zip] does not have quotes around the filename.  Placing the ContentType after the AddHeader will make it work correctly on a Mac as well.

Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
Response.ContentType = "application/" & IO.Path.GetExtension(filename)
Response.TransmitFile(physicalPath)
Response.End()
Avatar of geobaldi

ASKER

Thanks for the responses...making team is applying recommended code changes right now.  However, can someone explain why it works properly on some servers and not on others?
It could be caused by a couple of things.  The version of IIS running on the server, the version of ASP.NET that the website is running under, MIME types that are setup on the servers.  That's all I can think of at the moment.
This made it worse on the "misbehaving" server.  Previously, the filename was changed to "DownloadFile.html" and now the name is changed to "DownloadFile.aspx".  Thus, IE web browser is treating this as an .aspx file.  The actual file is till correct but the name is obviously wrong.  Fortunately, the other servers are still working properly.
        Response.Clear()
        Response.AddHeader("Content-Disposition", "attachment;filename=""" + fileName + """")
        Response.ContentType = "application/" & IO.Path.GetExtension(fileName)
        Response.TransmitFile(physicalPath)
        Response.AppendHeader("Pragma", "no-cache")
        Response.End()
    End Sub

Open in new window

It looks like you took bits and peices from everyones examples, just try the one that I gave you.

2 things.  You shouldn't need the response.Clear(), if you do then you need to specify Response.BufferOutput = True... You still have quotation marks around your """ + filename + """, those are not needed and may mess things up on certain browsers.
Response.AddHeader("Content-Disposition", "attachment;filename=" & fileName)
Response.ContentType = "application/" & IO.Path.GetExtension(filename)
Response.TransmitFile(physicalPath)
Response.End()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of geobaldi
geobaldi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial