在ASP.NET2.0中提供动态文件下载 [英] providing dynamic file download in ASP.NET2.0

查看:81
本文介绍了在ASP.NET2.0中提供动态文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供动态下载的文件。这些文件可以在服务器端实时生成,因此它们被表示为byte []并且不存在于磁盘上。我希望用户填写一个ASP.NET表单,点击下载按钮并返回他/她想要的文件。

I want to provide dynamic download of files. These files can be generated on-the-fly on serverside so they are represented as byte[] and do not exist on disk. I want the user to fill in an ASP.NET form, hit the download button and return the file he/she wanted.

这是我的代码在ASP后面。 NET窗体看起来像:

Here is how my code behind the ASP.NET form looks like:

public partial class DownloadService : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void submitButtonClick(object sender, EventArgs e)
{
    if (EverythingIsOK())
    {
        byte[] binary = GenerateZipFile(); 
        Response.Clear();
        Response.ContentType = "application/zip";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.BinaryWrite(binary);
        Response.End();
    }
}
...
}

我预计这段代码只是工作。我清除了Respone,放在我生成的zip文件和宾果。然而,这种情况并非如此。我在浏览器中收到以下消息:

I expected this piece of code just work. I clear the Respone, put in my generated zip file and bingo. However, this is not the case. I get the following message in the browser:

无法显示XML页面
无法使用样式表查看XML输入。请更正错误,然后单击刷新按钮,或稍后再试。
在文本内容中找到无效的字符。处理资源时出错 http:// localhost:15900 / mywebsite / DownloadS ...

The XML page cannot be displayed Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later. An invalid character was found in text content. Error processing resource 'http://localhost:15900/mywebsite/DownloadS...

我做错了什么?

推荐答案

以下是您需要进行的细微修改:

Here's a minor modification you need to make:

Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.BinaryWrite(binary);
Response.End();

这篇关于在ASP.NET2.0中提供动态文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆