使用AppendHeader时出现ParserError

时间:2011-10-17 14:12:07

标签: c# asp.net download

我正在调用一种方法,我希望让用户从我的服务器下载文件:

string path = MapPath(filePath);
     string name = Path.GetFileName(path);
     string ext = Path.GetExtension(path);
     string type = "";
     // set known types based on file extension  
     if (ext != null) {
        switch (ext.ToLower()) {
           case ".pdf":
              type = "Application/pdf";
              break;
        }
     }
     Response.AppendHeader("content-disposition",
         "attachment; filename=" + name);

     if (type != "") {
        Response.ContentType = type;
     }
     Response.WriteFile(path);
     Response.End(); 

我收到以下错误:

Foutdetails webpagina

Gebruikersagent:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 5.1; Trident / 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4 .0E) Tijdstempel:星期一,2011年10月17日14:09:42 UTC

Bericht:Sys.WebForms.PageRequestManagerParserErrorException:Het bericht dat是ontvangen van de server,是niet geparseerd 。 Vaak wordt dit probleem veroorzaakt door het feit dat het antwoord isananpast met aanroepen naar Response.Write(),antwoordfilters,HttpModules,of door het feit dat servertracering ingeschakeld。 详情:Er is eout fout opgetreden tijdens het parseren dichtbij%PDF-1.6 % 1246 0 雷格尔:4723 特肯:21 代码:0 URI:http://localhost:8080/ScriptResource.axd?d=pIR8OXvZoRojyW4g1LrIxQJ_PBSxowcac32J1wj6iuXDcoXm42o2_-RXqxdcfui11rd_ENAmsJBy6ZuWa6y59BIFz75plfDb9hceJY8IfLYyZk116lcE26DPuxB8-NB46RAswwOW4RN2EGnFK-0MTXrgj2R-IGGgcAS9lqbJpejfFHbU0&t=ffffffffdf4bca0c

有关如何解决此类错误的任何建议?

1 个答案:

答案 0 :(得分:1)

如果您是通过UpdatePanel通过AJAX执行此操作,则需要将按钮添加为PostBackTrigger。你也可以在代码隐藏中添加按钮作为回发控件,如下所示:

ScriptManager.GetCurrent(Page).RegisterPostBackControl(Button1);