1

当我尝试从我的 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();
    }
4

2 回答 2

2

我认为您正在更新面板中发送点击事件。如果您使用的是 ajax,请尝试在下载按钮单击事件上进行整页回发

于 2012-12-11T10:52:21.790 回答
0

您必须清除页面的所有html内容,并且只留下第一行(页面)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="download.aspx.vb" Inherits="download" %>
于 2012-12-11T10:54:46.207 回答