How to name a file for download on Firefox?
Asked Answered
N

5

2

The following headers work on IE but not on FF

<%@ Page Language="C#" ContentType="text/csv" Inherits="System.Web.Mvc.ViewPage"%>
<% Response.AddHeader("Content-Disposition", "filename=report.csv;attachment"); %>

In FF the suggested name in FF appears as "report" without the extension.

Nevels answered 20/7, 2009 at 14:49 Comment(0)
I
3

I am currently using code that looks like this:

response.AddHeader("Content-Disposition", "attachment; filename=\"data.csv\"");

In my daily work - and that works just fine. Also are you sure this is not your OS or anything that has the "Hide extensions for known file types"-option enabled? (I know Windows have this option and it drives me crazy)

Impeccant answered 20/7, 2009 at 15:12 Comment(2)
Maybe because he linked the documentationNevels
Possibly, idk... I think the solution is to use the quotes (being escaped) but I honestly don't know - I posted this as I knew it worked for me, I have no link to documentation or anything as it is code I created months and months ago and have used ever since without any issues.Impeccant
S
4

filename is just an parameter of Content-Dispostion. So you have to swap both:

<% Response.AddHeader("Content-Disposition", "attachment;filename=report.csv"); %>
Spotlight answered 20/7, 2009 at 14:55 Comment(0)
I
3

I am currently using code that looks like this:

response.AddHeader("Content-Disposition", "attachment; filename=\"data.csv\"");

In my daily work - and that works just fine. Also are you sure this is not your OS or anything that has the "Hide extensions for known file types"-option enabled? (I know Windows have this option and it drives me crazy)

Impeccant answered 20/7, 2009 at 15:12 Comment(2)
Maybe because he linked the documentationNevels
Possibly, idk... I think the solution is to use the quotes (being escaped) but I honestly don't know - I posted this as I knew it worked for me, I have no link to documentation or anything as it is code I created months and months ago and have used ever since without any issues.Impeccant
R
2

Recently i faced this issue too, finally got the solution

To make It work correctly in firefox, make sure in file name Quotes are placed correctly and with no Spaces in file name

so here is the sample example to do basic test

This will work:

Response.ClearContent();
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment;filename="+ "MyOrders"+ "_Date_"
+DateTime.Now.ToString("d")+".csv");

This also will work:

string myfilename = "MyOrders" + "_Date_" + DateTime.Now.ToString("d") + ".csv";
Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}", 
myfilename));

//here you can check using the break point,weather you are using any extra quotes in filename

This will not work in firefox

Response.AddHeader("Content-Disposition", "attachment;filename="+ "MyOrders"+ "_Date_"
+DateTime.Now+".csv");

// because in DateTime.Now there is spaces in Time, Seconds , so firefox downloads file not 
//as csv but downloads it as File , may be a binary file or unknown file type

References used this , this, this and this

Thanks to all whose post's helped somehow.

Hope it may help someone.

Radiolucent answered 3/9, 2015 at 10:24 Comment(0)
C
1

This question about CSV generation helped me when I needed to implement CSV generation and download: How do I best generate a CSV (comma-delimited text file) for download with ASP.NET?

Cumbrance answered 20/7, 2009 at 15:10 Comment(1)
thank you for the post, it helped to solve an issue with firefox here is the solutionRadiolucent
W
0

try "attachment; filename=\"report.csv\"" (i.e. with quoted filename)

Wages answered 20/7, 2009 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.