我正在尝试从一个从jquery事件调用的.ashx文件导出到excel。我的数据准备得很好。但系统不会提示我保存。如果我使用jquery事件,它可以很好地工作,但是从asp.net中,数据返回到调用jquery事件。由于需要花费很长时间来解释,我必须使用ashx代码。我该怎么办?
context.Response.ClearHeaders();
context.Response.Clear();
context.Response.ClearContent();
string attachment = "attachment; filename='UserRolesPermissions.xls'";
context.Response.AddHeader("content-disposition", attachment);
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.Write(excelData);
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();我尝试过使用Flush、End和CompleteRequest的各种组合。相同的结果(End错误输出)。
谢谢。
发布于 2018-08-18 05:40:02
您似乎缺少content-length标头。这可能会有所不同。
//make sure the data is a byte array
byte[] bin = excelData;
context.Response.ClearHeaders();
context.Response.Clear();
context.Response.Buffer = true;
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AddHeader("content-disposition", "attachment; filename=\"UserRolesPermissions.xls\"");
//this could help
context.Response.AddHeader("content-length", bin.Length.ToString());
context.Response.OutputStream.Write(bin, 0, bin.Length);
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();https://stackoverflow.com/questions/51903046
复制相似问题