基于此:ASP.Net Download file to client browser
这也应该有效:(contenu
是一个字符串)
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + txtFileName.Value.ToString() + "\"");
Response.AddHeader("Content-Length", contenu.Length.ToString());
Response.ContentType = "text/plain";
Response.Flush();
Response.Write(contenu);
Response.End();
然而,在Response.End()
之后,浏览器似乎做出了反应,因为我看到它正常工作,但后来没有任何反应......
Chrome和资源管理器相同。
我该怎么办?
答案 0 :(得分:2)
尝试使用稍微不同的参数设置的代码;这对我有用
protected void ExportData(string fileName, string fileType, string content)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlPathEncode(fileName));
Response.Charset = "";
Response.ContentType = fileType;
Response.Output.Write(content);
Response.Flush();
Response.End();
}
像这样打电话
ExportData("Report.txt", "text/plain", yourContentString);