【发布时间】:2015-06-16 19:38:52
【问题描述】:
我正在尝试允许用户下载 csv 文件,并使用 Excel 打开它(最好已选择“使用 Excel 打开文件”选项)。
//Cont.cs
public ActionResult CSVLink(string file)
{
var dir = Server.MapPath("/CSV/Stats");
var path = Path.Combine(dir, file + ".csv"); // I have tried this both with and w/o adding the csv here, and on the anchor's href instead
return File(path, "application/csv");
}
//Links.cshtml
<a target="_blank" href="/CSVLink?file=Hits_20150616">Today's Hits</a>
这主要是有效的,因为它允许用户下载文件,但文件扩展名不保持为 csv。问题似乎出在return File(path, "application/csv");,更具体地说是application/csv。
以下是各种 mime 类型发生的情况的列表:
-
application/csv- 文件下载,但没有文件扩展名 -
text/csv- 一样 -
application/binary- 一样 -
application/vnd.ms-excel- 文件下载为 .xls 文件,这会破坏它在 Excel 中的外观。
这是另一个奇怪的事情:所有没有扩展名的文件,如果我重命名它们并将 .csv 放在最后,它们在 Excel 中打开看起来很完美。这在 IE 和 Firefox 中都会发生(octet-stream 除外,这使它在新网页中以纯文本形式打开)。
我做错了什么?
【问题讨论】:
标签: c# mime-types