当我尝试从我的 asp.net 页面发送文件时出现此错误:
Microsoft JScript 运行时错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器接收到的消息。
我的代码:
linkButton
// 这由网格上的 a触发
protected void Download(object sender, EventArgs e)
{
LinkButton lb = sender as LinkButton;
// save file to temp
Byte[] fileBytes = null;
using (var db = new DbContext())
{
var id = Convert.ToInt32(lb.CommandArgument);
fileBytes = db.Requests.Single(x => x.Id == id).File;
}
var filePath = Path.GetTempFileName();
File.WriteAllBytes(filePath, fileBytes);
// send
FileInfo fi = new FileInfo(filePath);
SendFile(fi);
}
private void SendFile(FileInfo file)
{
Response.ContentType = "application/zip";
Response.WriteFile(file.FullName);
Response.End();
// I also tried the code below I get the same error.
//Response.Clear();
//Response.ClearHeaders();
//Response.ClearContent();
//Response.AddHeader("Content-Disposition", "attachment; filename=PriceFile.zip");
//Response.AddHeader("Content-Length", file.Length.ToString());
//Response.ContentType = "application/zip";
//Response.Flush();
//Response.TransmitFile(file.FullName);
//Response.End();
}