下载带有响应的项目

the*_*ion 1 c# asp.net download web

我想允许客户端下载附件。这attachment.AttachmentContent是文件中的字节数组。当我单击按钮下载附件时,它会运行下面的代码。但我有以下脚本错误。我应该做什么来修复它?

\n\n
\n

未捕获的 Sys.WebForms.PageRequestManagerParserErrorException:\n Sys.WebForms.PageRequestManagerParserErrorException: 无法解析从服务器接收到的消息。导致此错误的常见原因是通过调用 Response.Write() 修改响应、启用响应筛选器、HttpModule 或服务器跟踪。详细信息:\n 在 \'\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdJFIF``\' 附近解析时出错。

\n
\n\n
Attachment attachment = _attachmentService.GetAttachmentBytesById(int.Parse(e.CommandArgument.ToString()));\n\nResponse.Clear();\nResponse.ClearHeaders();\nResponse.ContentType = "image/jpeg";\nResponse.AddHeader("Content-Disposition", "attachment; filename=\\"" + "test.jpg" + "\\"");\nResponse.OutputStream.Write(attachment.AttachmentContent, 0, attachment.AttachmentContent.Length);\nResponse.End();\n
Run Code Online (Sandbox Code Playgroud)\n

nun*_*cal 6

看来您正在发送此文件下载作为部分响应。

最常见的原因是您的下载按钮位于 UpdatePanel 内。

文件只能通过完整回发发送。

您可以将下载按钮设置为回发触发器来解决此问题。

<Triggers>
    <asp:PostBackTrigger ControlID="Download_Click">
</Triggers>
Run Code Online (Sandbox Code Playgroud)