We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Filename changes when downloading file with ASP.NET and IIS

Medium Priority
1,806 Views
Last Modified: 2013-11-07
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

Comment
Watch Question

Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
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()

Author

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?
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.

Author

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

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

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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.