如何在浏览器中打开MemoryStream文件?

发布于 2024-12-01 04:13:07 字数 446 浏览 0 评论 0原文

我想在我的 Web 应用程序的代码隐藏文件中创建一个文本文件。但是,我不允许将此文件保存到服务器。所以我尝试使用 MemoryStream 类将文件保存到内存中。到目前为止,我

MemoryStream memoryStream = new MemoryStream();
TextWriter textWriter     = new StreamWriter(memoryStream);
textWriter.WriteLine("Something");
memoryStream.Close();

似乎可以工作,但我的要求是当他/她单击按钮时在客户端浏览器上打开此文件。由于该文件没有像.../text.txt 这样的物理路径。我不知道如何在浏览器上打开它。

我如何使用 C# 在 ASP.Net 中执行此操作。我进行了很多搜索,但找不到适合我的解决方案。

提前致谢。

I want to create a Text file in my code behind file in my web application. However, I am not allowed to save this file to server. So I tried to use MemoryStream class to save my file into memory. So far I have,

MemoryStream memoryStream = new MemoryStream();
TextWriter textWriter     = new StreamWriter(memoryStream);
textWriter.WriteLine("Something");
memoryStream.Close();

It seems like working but my requirement is to open this file on client browser when he/she clicks button. Since this file does not have a physical path like ..../text.txt. I have no idea how to open it on browser.

How can I do this in ASP.Net using C#. I searched a lot but could not find a solution working for me.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

错々过的事 2024-12-08 04:13:07

这比你想象的要容易得多。请记住,HTTP 协议实际上并不传输严格意义上的“文件”。它传输请求和响应,每个请求和响应都包含标头和内容。在这种情况下,您关心的是响应的标题和内容。

在 WebForms 应用程序中执行此操作的最简单方法是使用通用处理程序。具体来说,看看该链接中处理程序响应的实现:

context.Response.ContentType = "image/png";
context.Response.WriteFile("~/Flower1.png");

这是在适当设置响应的标头后将图像文件的内容写入响应。您想要的更接近实现中注释掉的内容:

context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");

这将向浏览器发送纯文本,仅此而已。浏览器不会认为它是一个网页或类似的东西,不会对其应用任何样式等。就网络浏览器而言,它只是下载了一个文本文件,其中包含“Hello World”字样。您可以Response.Write()写入您想要构建该文件的所有文本。

您可以进一步自定义响应标头,为浏览器提供更多信息。例如,如果将以下标头添加到 HttpResponse

Content-Disposition: attachment; filename=myfile.txt

然后浏览器会将其翻译为应该下载并保存该“文件”,而不仅仅是显示。 (当然,用户的浏览器设置可能会告诉它无论如何都要显示它,但这是服务器“建议”浏览器应该保存文件的正确方法。)

从浏览器的角度来看,它“文件”来自哪里并不重要。无论它是来自服务器的文件系统还是动态生成或神奇地变出,都没有区别。浏览器只关心响应头和内容。如果标题说它是文本,并且它们说它是文件,那么内容将被视为文本文件。

This is a lot easier than you think. Keep in mind that the HTTP protocol doesn't actually transfer "files" in the strictest sense of the word. It transfers requests and responses, each containing headers and content. In this case, you're concerned with the headers and content of the response.

The easiest way to do this in a WebForms application is with a generic handler. Specifically, take a look at the implementation of the handler's response in that link:

context.Response.ContentType = "image/png";
context.Response.WriteFile("~/Flower1.png");

This is writing the content of an image file to the response after setting the response's header appropriately. What you want is closer to what's commented out in the implementation:

context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");

This would send to the browser plain text, nothing more. The browser won't think it's a web page or anything like that, won't apply any styles to it, etc. As far as the web browser is concerned, it just downloaded a text file with the words "Hello World" in it. You can Response.Write() all the text you want to build that file.

You can further customize your response headers to give the browser even more information. For example, if you add the following header to your HttpResponse:

Content-Disposition: attachment; filename=myfile.txt

Then the browser will translate that to mean that this "file" should be downloaded and saved, not just displayed. (Of course, the user's browser settings may tell it to display it anyway, but this is the proper way for the server to "suggest" to the browser that it should save the file.)

From the point of view of the browser, it doesn't matter where the "file" came from. Whether it was from the server's file system or dynamically generated or magically conjured, it makes no difference. The browser is concerned only with the response headers and content. If the headers say it's text, and they say that it's a file, then the content will be treated as a text file.

莫相离 2024-12-08 04:13:07

为什么需要写MemoryStream?如果您想将其发送到浏览器,只需将其写入 HTTP 响应即可。

Response.WriteLine("Something");

如果您想让浏览器将此响应下载为文件,请参阅此处

Why do you need to write a MemoryStream? Just write it to the HTTP response if you want to send it to the browser.

Response.WriteLine("Something");

If you want to make the browser download this response as a file, see here.

梦屿孤独相伴 2024-12-08 04:13:07

老实说,我相信这在 Web 开发中不是一个好的模式。

它只是读取文件并将其数据以文本形式发送到客户端(Web 浏览器),在文本框中对其进行编辑,发回修改后的文本并将其另存为文件在您选择的路径或存储中。

HTTP 是一种无状态协议,因此在客户端编辑文件内容时,您不会在服务器端打开文件,因为在服务器响应结束后,两层都完全断开连接。

I honestly believe this isn't a good pattern in Web development.

It's just about reading your file and send its data as text to the client-side (Web browser), edit it in a textbox, send back modified text and save it as file in the path or storage of your choice.

HTTP is an stateless protocol, so you won't be leaving a file opened in the server-side while its contents are edited in client-side, as both tiers are absolutely disconnected after server response ends.

假扮的天使 2024-12-08 04:13:07

好吧,我想我明白你想要什么了。你说你有一个按钮,你想转到一个文本文件的内容,你想在内存中创建该文件,但你不知道当用户单击该按钮时将浏览器发送到哪个网址?

如果是这种情况,您可以执行以下操作:

1) On the page that has the button, set the href (or link-location or whatever) of the button to be a new asp.net page (jet to be created). Something like "textfile.aspx" or whatever. Also, remove all the code regarding the memory-stream.
2) Create the new asp.net file (textfile.aspx, or whatever you decided to call it). The content of that file should be like this:

Response.WriteLine("Something"); // Or whatever you previously wrote to the MemoryStream

重点是,您应该分成两个不同的文件(或基于查询字符串的单独操作)。

Ok, I think I figured out what you want. You say you have a button, with witch you want to go to the content of a text-file, that you want to create in-memory, but you don't know what url to send the browser to when the user clicks the button?

If this is the case here's what you can do:

1) On the page that has the button, set the href (or link-location or whatever) of the button to be a new asp.net page (jet to be created). Something like "textfile.aspx" or whatever. Also, remove all the code regarding the memory-stream.
2) Create the new asp.net file (textfile.aspx, or whatever you decided to call it). The content of that file should be like this:

Response.WriteLine("Something"); // Or whatever you previously wrote to the MemoryStream

The point is, you should separate into two different files (or separate action based on query-string).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文