asp.net C MVC 4导出到CSV C 另存为对话框在Chrome和Firefox中不起作用
我的问题是类似于how-to-force-chrome-to-open-an-open-file-dialog-when-downloading-a-file-via-as(它正在Chrome和Firefox中下载文
|
我正在尝试使用打开/保存选项将csv文件导出到用户. 我的问题是类似于how-to-force-chrome-to-open-an-open-file-dialog-when-downloading-a-file-via-as(它正在Chrome和Firefox中下载文件),我尝试了@Dev建议的解决方案,但它无法正常工作. 我编写了如下代码: C return File(new System.Text.UTF8Encoding().GetBytes(csvData),"text/csv",filename);但是,它无法在Chrome中运行.默认情况下会下载该文件. 然后在谷歌搜索后,我找到了returning-a-file-to-view-download-in-mvc,我试图做下面的事情: C 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,其中@JooSimes提到: C That is browser dependent. If you set to download automatically to a 如果以上是真的,那么我怎样才能克服我的问题呢?如何进行打开/保存对话? 编辑1 我试图做这样的事情(得到它here): C 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)); } }和我的控制器代码: C return new ExcelResult { FileName = "sample.xls",Path = "",Data = csvData };但是,它正在下载Excel … 编辑2 尝试用HttpContext.Current.Response打开excel /// <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); } }但是,仍然没有工作! 解决方法 @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/ (编辑:日照站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 如何将复选框绑定到视图模型的List属性?
- Asp.Net的FileUpload类实现上传文件实例
- asp.net-mvc – 使用ASP.Net MVC中的模型绑定器更新父/子记
- asp.net – Intranet / Internet的Windows身份验证
- asp.net – Tridion分析和个性化错误:用户不能为空.请确保
- asp.net – Silverlight初始化错误2110 Internet Explorer
- asp.net C 是否有可能过滤SignalR中的接收器?
- asp.net-mvc – 图像URL中的符号MVC导致一个潜在危险的Requ
- asp.net – Windows应用程序与Web应用程序开发
- asp.net-mvc – ASP.net MVC:在RenderAction中获
- 为什么asp.net将页面包装在一个表单中?
- asp.net-mvc – 是否有一个ASP MVC与JSTL标签等效
- asp.net-mvc – 在EditorFor for child对象中使用
- asp.net – Mocking HttpContext不起作用
- asp.net – 有谁知道如何摆脱我的.net网络服务的
- ASP.Net MVC cookies – 防篡改?
- ASP.NET对大文件上传的解决方案
- asp.net-web-api – ASP身份OAuth令牌 – 我应该
- asp.net-mvc – 在asp.net mvc 3剃刀中识别html助
- asp.net-mvc – 使用复杂类型嵌套对象的bind属性
- asp.net-mvc C 已经使用相同的参数类型定义
- ASP.Net C AJAX UpdatePanel中的Javascript
- asp.net-mvc C ASP.NET MVC中的WebApi [Fro
- asp.net-core C 如何使用ASP.NET注册OData
- asp.net C MVC4 C ContextDependentView C
- asp.net-mvc-3 C 如何从ASP.NET MVC#输出中
- asp.net C 适用于多个用户的EWS通知中心
- asp.net-mvc C 未在ELMAH中记录的错误
- asp.net-mvc-4 C 在EF迁移配置类的Seed方法
- asp.net-mvc C 使用AD的ASP.NET MVC表单Aut
