我需要将文件保存为客户端计算机中的.csv。我正在使用下面的代码。
protected void btnGen_Click(object sender, EventArgs e)
{
try
{
string fileName = "test";
string attachment = "attachment; filename=" + fileName + ".csv";
List<string> lst1 = new List<string> { "wert", "fds", "hjgfh", "hgfhg" };
List<string> lst2 = new List<string> { "hgfds", "hdfg", "yretg", "treter" };
List<string> lst3 = new List<string> { "hgfdgf", "gfdg", "gfdgf", "ghfdg" };
List<List<string>> lst = new List<List<string>> { lst1, lst2, lst3 };
StringBuilder sb = new ExportFacade().GenerateStringBuilder(lst);
Response.ContentType = "text/csv" ;
lblProgress.ForeColor = System.Drawing.Color.Green;
lblProgress.Text = "File created and write successfully!";
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
Response.Write(sb.ToString());
lblProgress.ForeColor = System.Drawing.Color.Green;
lblProgress.Text = "File created and write successfully!";
Response.Flush();
Response.End();
}
catch (Exception ex)
{
lblProgress.ForeColor = System.Drawing.Color.Red;
lblProgress.Text = "File Saving Failed!";
}}
我不使用更新面板。
当我单击该按钮时,会得到以下错误
Sys.WebForms.PageRequestManagerParserErrorException:从服务器接收的消息无法解析。导致此错误的常见原因是,当响应被Response.Write()调用修改时,响应筛选器、HttpModules或服务器跟踪被启用。
如果你能帮我解决这个问题,那将对我有很大的帮助。谢谢。
发布于 2011-10-04 04:05:46
我解决了。母版页中的Ajax脚本管理器造成了问题。在移除它之后,该函数正常工作。但是标签没有被更新,因为响应不是针对页面的。
https://stackoverflow.com/questions/7552577
复制相似问题