如何通过单击 ASP.NET 中的图像按钮来传输 zip 文件?
我的问题:当用户单击 aspx 页面上的图像按钮时,代码隐藏会创建一个 zip 文件,然后我尝试将该 zip 文件流式传输给用户。
要流式传输文件,我使用以下代码:
FileInfo toDownload = new FileInfo(fullFileName);
if (toDownload.Exists)
{
Response.Clear();
Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment;filename=" +
toDownload.Name);
Response.AppendHeader("Content-Length", toDownload.Length.ToString());
Response.TransmitFile(fullFileName);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
当我尝试执行此代码时,我在页面上收到以下错误:
Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。 出现此错误的常见原因是通过调用 Response.Write()、响应筛选器、HttpModules 或启用服务器跟踪来修改响应。 详细信息:解析“PK...”附近时出错。
PK 是 zip 文件中的前两个字符,用于将其标识为 zip 文件,因此我知道它正在尝试将 zip 文件发送到浏览器。 但是,我的印象是浏览器正在尝试解释和/或渲染 zip 文件,而我希望它弹出下载文件选项。
有想法吗?
My problem: When a user clicks an image button on an aspx page the codebehind creates a zip file and then I attempt to stream that zip file to the user.
To stream the file I'm using the following code:
FileInfo toDownload = new FileInfo(fullFileName);
if (toDownload.Exists)
{
Response.Clear();
Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment;filename=" +
toDownload.Name);
Response.AppendHeader("Content-Length", toDownload.Length.ToString());
Response.TransmitFile(fullFileName);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
When I try to execute this I'm getting the following error on the page:
Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'PK...'.
The PK are the first two characters in a zip file that identify it as a zip file so I know that it's attempting to send the zip file to the browser. However, I get the impression that the browser is trying to interpret and/or render the zip file while I want it to pop-up a download file option.
Ideas?
EDIT: Here's a link to a post from the guy who wrote the above error message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
例如,以下是我在我的一个应用程序中向客户端发送 PDF 的方式(您必须填写/更改一些缺少的变量声明):
您需要更改内容类型,并将 zip 文件转换为字节数组,然后我想你可以填写其余的。
For example, here's how I send a PDF to the client in one of my apps (you'll have to fill in/change some of the missing variable declarations):
You'd change the content type, and convert your zip file to a byte array, and then I think you can fill in the rest.
我终于解决了问题,还注意到也许我没有在问题中提供足够的信息:图像按钮位于 UpdatePanel 内。
解决方案是为控件创建一个 PostBackTrigger:
I finally solved the problem and also noticed that perhaps I didn't give enough information in the question: The image button is inside an UpdatePanel.
The solution was to create a PostBackTrigger for the control:
伙计,你不是使用 DotNetZip 来生成 zip 文件吗? 如果您没有在磁盘上创建 zip 文件而仅在内存中创建该怎么办? 本示例使用 DotNetZip 来执行此操作。
Guy, aren't you using DotNetZip to generate the zip files? What if you didn't create the zip file on disk but only in memory? This example uses DotNetZip to do that.