简体   繁体   English

C#ASP。 NET内联文件下载处理程序

[英]C# ASP. NET Inline file download handler

I'm trying to create a file handler for users to download files when their filenames are clicked on a web page. 我正在尝试创建一个文件处理程序,供用户在网页上单击其文件名时下载文件。 I've implemented this a few times without issues, but I'm currently getting an error which I can't get my head around. 我已经实现了几次,没有任何问题,但是我现在遇到一个错误,我无法理解。

Code: 码:

protected void btnViewFile_Click(object sender, EventArgs e)
{
    var btnViewFile = sender as LinkButton;
    Response.Clear();
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + btnViewFile.CommandArgument.ToString());
    Response.WriteFile(Server.MapPath(btnViewFile.CommandArgument));
    Response.End();
}

If I look at the browser console, I can see: 如果查看浏览器控制台,可以看到:

Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. 未捕获的Sys.WebForms.PageRequestManagerParserErrorException:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。

No exceptions appear to be thrown in the code, the requested file is converted into the correct full path; 代码中似乎没有引发异常,所请求的文件将转换为正确的完整路径; and I've tried quite a few different things - clearing headers manually, flushing before ending, giving a more explicit content-type header, using AddHeader instead of AppendHeader, using TransmitFile rather than WriteFile, and quite a bit more. 我已经尝试了很多不同的方法-手动清除标头,在结束之前刷新,提供更明确的内容类型标头,使用AddHeader而不是AppendHeader,使用TransmitFile而不是WriteFile,等等。

Any ideas? 有任何想法吗?

In case anyone else comes across this situation, the problem was that I was registering it as a postback control in ScriptManager, not an async postback control. 万一其他人遇到这种情况,问题是我将其注册为ScriptManager中的回发控件,而不是异步回发控件。

D'oh! D'哦!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM