JoelCool
JoelCool

Reputation: 343

How to get asp.net to download dynamic csv

ENV: Asp.Net Vb / Visual Studio 2010 / .Net 4 IIS Express and IIS 6

I have a page called download.aspx which creates csv data from a database but cannot get browsers to download it with the right file name. My understanding was that Content-Disposition gives it a file name but the download is always the name of my page instead which is download.aspx.

I'm sure I have a misunderstanding of how this works and I've searched here for guidance but can't seem to get any solutions to work. I have the following:

    Response.Clear()
    Response.ContentType = "text/csv"
    Response.AppendHeader("Content-Disposition", "attachment: filename=leadership.csv")
    Response.Write("test,test,test")
    Response.Flush()
    Response.End()

I have tried "application/csv" and "application/x-download" with no differences. I have also tried it in a .ashx file with same file name issue. How can I get the file to come down as leadership.csv instead of download.aspx?

Upvotes: 2

Views: 927

Answers (1)

user166390
user166390

Reputation:

The Content-Dispostion header is wrong. The value

attachment: filename=leadership.csv

should be (with optional space)

attachment; filename=leadership.csv

Note that the :, which is used to separate headers from values, is changed to a ;, which is used to separate sub-values.

In addition, I would recommend using a library to generate the CSV, if not already the case ..

Upvotes: 2

Related Questions