Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSV downloads as HTM

Tags:

c#

csv

asp.net

web

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?

like image 980
user1466569 Avatar asked Nov 03 '22 21:11

user1466569


1 Answers

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)));
like image 119
Paul Turner Avatar answered Nov 09 '22 07:11

Paul Turner