是否可以通过mvcgrid更改csv下载上的文件扩展名?它以.csv格式下载,我希望.txt阻止我的用户在excel中打开内容?
我可以看到有一个自定义的渲染引擎,但这似乎提供了文件内容格式而不是扩展名。
谢谢
答案 0 :(得分:0)
通过自定义呈现引擎对其进行管理
using MVCGrid.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace MVCGrid.Web.Models
{
public class TextExportEngine : IMVCGridRenderingEngine
{
public bool AllowsPaging
{
get { return false; }
}
public void PrepareResponse(HttpResponse httpResponse)
{
httpResponse.Clear();
httpResponse.ContentType = "text/comma-separated-values";
httpResponse.AddHeader("content-disposition", "attachment; filename=\"" + "export" + ".txt\"");
httpResponse.BufferOutput = false;
}
public void Render(MVCGrid.Models.RenderingModel model, MVCGrid.Models.GridContext gridContext, System.IO.TextWriter outputStream)
{
var sw = outputStream;
StringBuilder sbHeaderRow = new StringBuilder();
foreach (var col in model.Columns)
{
if (sbHeaderRow.Length != 0)
{
sbHeaderRow.Append(",");
}
sbHeaderRow.Append(Encode(col.Name));
}
sbHeaderRow.AppendLine();
sw.Write(sbHeaderRow.ToString());
foreach (var item in model.Rows)
{
StringBuilder sbRow = new StringBuilder();
foreach (var col in model.Columns)
{
var cell = item.Cells[col.Name];
if (sbRow.Length != 0)
{
sbRow.Append(",");
}
string val = cell.PlainText;
sbRow.Append(Encode(val));
}
sbRow.AppendLine();
sw.Write(sbRow.ToString());
}
}
private string Encode(string s)
{
if (String.IsNullOrWhiteSpace(s))
{
return "";
}
return s;
}
public void RenderContainer(MVCGrid.Models.ContainerRenderingModel model, System.IO.TextWriter outputStream)
{
}
}
}
,然后将以下内容添加到我的网格定义中
.AddRenderingEngine("tabs", typeof(TextExportEngine)