C# 在 ASP.NET 中将文本下载为文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14755339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:55:06  来源:igfitidea点击:

Download text as file in ASP.NET

c#asp.net.net

提问by Vijay

I am trying to download some text output from the screen as a text file. Following is the code. It's working on some pages and not working at all on other pages. Can anyone please suggest what's wrong here?

我正在尝试从屏幕下载一些文本输出作为文本文件。以下是代码。它在某些页面上工作,而在其他页面上根本不工作。任何人都可以请建议这里有什么问题吗?

protected void Button18_Click(object sender, EventArgs e){
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "text/plain";
    Response.AppendHeader("content-disposition", "attachment;filename=output.txt");

    StringBuilder sb = new StringBuilder();
    string output = "Output";
    sb.Append(output);
    sb.Append("\r\n");
    Response.Write(sb.ToString());
}

采纳答案by Rui Jarimba

As already mentioned by Joshua, you need to write the text to the output stream (Response). Also, don't forget to invoke Response.End() after that.

正如 Joshua 已经提到的,您需要将文本写入输出流(响应)。另外,不要忘记在那之后调用 Response.End() 。

protected void Button18_Click(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    string output = "Output";
    sb.Append(output);
    sb.Append("\r\n");

    string text = sb.ToString();

    Response.Clear();
    Response.ClearHeaders();

    Response.AppendHeader("Content-Length", text.Length.ToString());
    Response.ContentType = "text/plain";
    Response.AppendHeader("Content-Disposition", "attachment;filename=\"output.txt\"");

    Response.Write(text);
    Response.End();
}

Edit 1: added more details

编辑 1:添加更多细节

Edit 2: I was reading other SO posts where users were recommending to put quotes around the filename:

编辑 2:我正在阅读其他 SO 帖子,其中用户建议在文件名周围加上引号:

Response.AppendHeader("content-disposition", "attachment;filename=\"output.txt\"");

Source: https://stackoverflow.com/a/12001019/558486

来源:https: //stackoverflow.com/a/12001019/558486

回答by Joshua

If that's your actual code, you never write the text to the response stream, so the browser never receives any data.

如果这是您的实际代码,则您永远不会将文本写入响应流,因此浏览器永远不会收到任何数据。

At the very least, you should need

至少,你应该需要

Response.Write(sb.ToString());

to write your text data to the response. Also, as an added bonus, if you know the length beforehand you should provide it using the Content-Lengthheader so the browser can show the download progress.

将您的文本数据写入响应。此外,作为额外的好处,如果您事先知道长度,您应该使用Content-Length标题提供它,以便浏览器可以显示下载进度。

You're also setting Response.Buffer = true;as part of your method but never explicitly flush the response to send it to the browser. Try adding a Response.Flush()after your write statement.

您还将设置Response.Buffer = true;作为方法的一部分,但从未明确刷新响应以将其发送到浏览器。尝试Response.Flush()在您的 write 语句之后添加一个。