Shu*_*ubh 5 asp.net download http-headers asp.net-mvc-4
我正在尝试csv
使用打开/保存选项将文件导出到用户.
我的问题是类似于how-to-force-chrome-to-open-an-open-file-dialog-when-download-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);
Run Code Online (Sandbox Code Playgroud)
但是,它无法在Chrome中运行.默认情况下会下载该文件.
然后在谷歌搜索后,我发现返回文件到视图下载在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");
Run Code Online (Sandbox Code Playgroud)
但它仍然是在Chrome中下载文件.然后我遇到了如何显示 - 打开 - 保存 - 对话框 - asp-net-mvc-4,其中@JoãoSimões
提到: -
这取决于浏览器.如果您设置为自动下载到给定文件夹,浏览器将自动下载.Firefox和Chrome是一些具有此行为的浏览器. - JoãoSimões1月3日13:09
如果以上是真的,那么我怎样才能克服我的问题呢?如何进行打开/保存对话?我无法使用打开/保存选项导出我的CSV.
编辑1
我试图做这样的事情(在这里得到): -
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));
}
}
Run Code Online (Sandbox Code Playgroud)
和我的控制器代码: -
return new ExcelResult
{
FileName = "sample.xls",
Path = "",
Data = csvData
};
Run Code Online (Sandbox Code Playgroud)
但是,它正在下载Excel ...
编辑2
尝试打开excel 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);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,仍然没有工作!
小智 2
@shubh您是否尝试过如何在通过 ASP.NET 代码隐藏下载文件时强制 Chrome 打开“打开文件对话框”?第二个解决方案他们将图像放在显示如何在 Chrome 中打开对话框的位置。我有 chrome 版本 30.0.1599.101 m,如果您进入该高级设置,然后向下您会找到上面链接答案中给出的复选框,我认为这将解决您的问题。如果仍然无法正常工作,则可能是您的浏览器有问题,只需将其更新到最新版本,然后重试。
编辑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";
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请检查此http://surajdeshpande.wordpress.com/2013/09/03/export-gridview-data-to-csv-file-in-asp-net/
归档时间: |
|
查看次数: |
5721 次 |
最近记录: |