• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1171
  • Last Modified:

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

0
geobaldi
Asked:
geobaldi
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
AddHeader needs the Content-filetype as well.
0
 
prairiedogCommented:
Add this before Response.AddHeader:
Response.Clear()
0
 
CB_ThirumalaiCommented:
Add this too before the Response.End();

Response.AppendHeader("Pragma", "no-cache");
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
aibusinesssolutionsCommented:
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()
0
 
geobaldiAuthor Commented:
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?
0
 
aibusinesssolutionsCommented:
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.
0
 
geobaldiAuthor Commented:
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

0
 
aibusinesssolutionsCommented:
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

0
 
geobaldiAuthor Commented:
After much investigation and calling on Microsoft Premier support, we identified the problem was with a critical file, "metabase.xml", being compressed.  Uncompressing the file and restarting the website resolved the issue.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now