C# Asp.net 向客户端写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1072814/
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-06 07:34:39  来源:igfitidea点击:

C# Asp.net write file to client

c#response

提问by jim

I hope this is a quick question I hope. I need to write some reports and then have the user prompted to save it to his/her local machine. The last time I did this I wrote a file to the webserver and then sent it to the client via Responseobject.

我希望这是一个我希望的快速问题。我需要编写一些报告,然后提示用户将其保存到他/她的本地计算机。上次我这样做时,我向网络服务器写入了一个文件,然后通过Response对象将其发送到客户端。

to create on the webserver

在网络服务器上创建

            TextWriter tw = new StreamWriter(filePath);

to send to client

发送给客户

           page.Response.WriteFile(path);

The question is, Is there a way to skip the writing of the physical file to to the webserver and go right from an object that represent the document to the response?

问题是,有没有办法跳过将物理文件写入网络服务器并直接从代表文档的对象到响应的方法?

采纳答案by Nikos Steiakakis

You could use the Response.ContentType like this

你可以像这样使用 Response.ContentType

Response.ContentType = "text/plain";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("Content-Disposition", "attachment;filename=yourfile.txt");

This of course works if you want to write a text file. In case you want to write a .doc for example you change the ContentType to "application/msword" etc...

如果您想编写文本文件,这当然有效。例如,如果您想编写 .doc,请将 ContentType 更改为“application/msword”等...

回答by Ganesh R.

You can.

你可以。

Try this:

尝试这个:

Table oTable = new Table();
//Add data to table.

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename="test.xls"");
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.Html32TextWriter oHtmlTextWriter = new System.Web.UI.Html32TextWriter(oStringWriter);
0Table.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();

This will give prompt the user to open or save test.xls file. similarly you can provide other ASP.NET objects in place of Table.

这将提示用户打开或保存 test.xls 文件。同样,您可以提供其他 ASP.NET 对象来代替 Table。

回答by Sani Singh Huttunen

Yes.

是的。

page.Response.WriteFile(yourData, 0, yourData.Length);

回答by Jouni Heikniemi

The exact answer to the question depends on how do you have your report organized (i.e. what is the "object" you're referring to). If you have a single string, you can use Response.Write to just write it. If you need a TextWriter, Response.Output is what you want - that way you can skip writing to the disk and then using WriteFile. Also, if your content happened to be binary, you could use Response.OutputStream for that.

问题的确切答案取决于您如何组织报告(即您所指的“对象”是什么)。如果您只有一个字符串,则可以使用 Response.Write 来编写它。如果您需要 TextWriter,Response.Output 就是您想要的 - 这样您就可以跳过写入磁盘然后使用 WriteFile。此外,如果您的内容碰巧是二进制的,您可以为此使用 Response.OutputStream。

You may also want to set Response.AddHeader("Content-Disposition", "attachment"), if you want the user to see a save file dialog. However, Content-Disposition is not necessarily honored, so the user may still get the report opened directly in the browser. Remember to set headers before you output the actual content!

如果您希望用户看到保存文件对话框,您可能还想设置 Response.AddHeader("Content-Disposition", "attachment")。但是,Content-Disposition 不一定会得到遵守,因此用户仍然可以直接在浏览器中打开报告。请记住在输出实际内容之前设置标题!

Also, depending on the format of the report, you may want to set Response.ContentType to something appropriate such as text/plain, text/csv or whatever have you. The correct mime types are listed at the IANA site.

此外,根据报告的格式,您可能希望将 Response.ContentType 设置为适当的内容,例如 text/plain、text/csv 或您拥有的任何内容。IANA 站点上列出了正确的 MIME 类型。