user1466569
user1466569

Reputation: 53

CSV downloads as HTM

I am having a huge problem in all browsers.

I have a site where clients can download a csv file that contains detail they need.

The problem I am having is that the csv file either downloads with no extension or as a htm file.

In the code I am specifying the file name with .csv, the file on the server is also a .csv.

The code is as follows

context.Response.Buffer = true;
context.Response.Clear();
context.Response.ClearHeaders();                    
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", @"attachment,     
     filename=" + ((string)Path.GetFileName(downloadFilePath)));
context.Response.WriteFile(downloadFilePath);
context.Response.Flush();
context.Response.Close();

I have tried context.Response.ContentType = "text/html"; and context.Response.ContentType = "application/octet-stream";.

It is running on IIS6.

Does anybody know what could be causing this?

Upvotes: 5

Views: 949

Answers (2)

user1466569
user1466569

Reputation: 53

Ok I have worked out why files are not downloading as CSV.

its because the file name has spaces in it, so I need to enclose the filename so that it doesnt cut off at the space.

Thank you for all your help

Upvotes: 0

Paul Turner
Paul Turner

Reputation: 39625

Assuming your verbatim string literal is on a single-line in your source, have you tried replacing the , in your Content-Disposition header with a ;? Examples I have found always use a semi-colon there.

It also might be safer to use quotes around your filename to protect the header from special characters:

context.Response.AppendHeader(
    "Content-Disposition",
    string.Format(
        "attachment; filename=\"{0}\"",
        Path.GetFileName(downloadFilePath)));

Upvotes: 2

Related Questions