我正在尝试使用“打开/保存”选项将“csv
文件导出到用户”。
我的问题是与how-to-force-chrome-to-open-an-open-file-dialog-when-downloading-a-file-via-as (It is downloading the file in Chrome and Firefox)
类似的问题,我尝试使用@Dev
建议的解决方案,但它无效。
我的代码编写如下: -
return File(new System.Text.UTF8Encoding().GetBytes(csvData),
"text/csv", filename);
但是,它无法在Chrome中运行。该文件默认下载。
然后在谷歌搜索后,我找到了returning-a-file-to-view-download-in-mvc,我试图做以下事情: -
var csvData = "hello";// I am filling this variable with ,y values from DB!
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = "test",
Inline = false,
};
Response.AppendHeader("Content-Disposition",
cd.ToString());
return File(new System.Text.UTF8Encoding().GetBytes(csvData),
"text/csv");
但仍然是在Chrome中下载文件。然后我遇到how-to-display-open-save-dialog-asp-net-mvc-4,其中@JoãoSimões
提到: -
这取决于浏览器。如果设置为自动下载到 给定文件夹,浏览器将自动下载。 Firefox和 Chrome是一些具有此行为的浏览器。 - JoãoSimões1月3日 13:09
如果以上情况属实,那么我该如何克服我的问题呢?如何进行打开/保存对话? 我无法使用打开/保存选项导出我的CSV。
修改1
我试图做这样的事情(得到它here): -
public class ExcelResult : ActionResult
{
public string FileName { get; set; }
public string Path { get; set; }
public string Data { get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Buffer = true;
context.HttpContext.Response.Clear();
context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
context.HttpContext.Response.ContentType = "text/csv";
context.HttpContext.Response.Write(new System.Text.UTF8Encoding().GetBytes(Data));
}
}
和我的控制器代码: -
return new ExcelResult
{
FileName = "sample.xls",
Path = "",
Data = csvData
};
但是,它正在下载Excel ...
修改2
尝试用HttpContext.Current.Response
/// <summary>
/// Export CSV
/// </summary>
/// <returns></returns>
public void DownloadCSV()
{
try
{
var csvData = Session["CSVData"].ToString();
byte[] getContent = new System.Text.UTF8Encoding().GetBytes(csvData);
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", getContent.Length.ToString());
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "testing.csv");
System.Web.HttpContext.Current.Response.BinaryWrite(getContent);
System.Web.HttpContext.Current.Response.Flush();
}
catch (Exception ex)
{
HttpResponseMessage message = new HttpResponseMessage()
{
Content = new StringContent("Error Exporting Data")
};
throw new System.Web.Http.HttpResponseException(message);
}
}
但是,仍然没有工作!
答案 0 :(得分:2)
@shubh你有没有试过How to force Chrome to open an "open file dialog" when downloading a file vía ASP.NET codebehind?第二个解决方案,他们把图像放在显示如何在chrome中打开对话框的位置。我有铬版本30.0.1599.101米,如果你去设置那个提前设置然后下来你会发现上面的链接答案给出的复选框,这将解决你的问题我想。
如果仍然无法正常工作,那么您的浏览器可能会出现问题,只需将其更新到最新版本然后重试。
修改1:
如果你把你的文件扩展名.xls然后它将在excel中为csv打开你需要将文件名设置为FileName = "sample.csv",
然后它将以csv格式打开。
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewtoCSVExport.csv");
Response.Charset = string.Empty;
Response.ContentType = "application/text";
了解更多信息http://surajdeshpande.wordpress.com/2013/09/03/export-gridview-data-to-csv-file-in-asp-net/
答案 1 :(得分:1)
如果用户已将其浏览器配置为自动下载文件,则无法在服务器上执行任何操作以强制显示此对话框。我担心你想要达到的目标是不可能的。
答案 2 :(得分:0)
尝试supply another value获取内容处置标题:
Response.AppendHeader("Content-Disposition", "attachment");