下面的方法,基于这个问题中的代码,在浏览器中显示一个文件下载对话框,但是下载永远不会开始(它保持在0%):
protected void lnkExport_Click(object sender, EventArgs e) {
var bytes = Encoding.ASCII.GetBytes(SelectRecords()); //Data to be downloaded
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename="test.xls"");
using (var stream = new MemoryStream(bytes)) {
Response.AddHeader("Content-Length", stream.Length.ToString());
stream.WriteTo(Response.OutputStream);
}
}
知道怎么回事吗?
您的代码对我来说工作得很好,但您可能想尝试将此添加为单击处理程序的最后一行:
Response.End();