开发者

How to Open MemoryStream File In Browser?

开发者 https://www.devze.com 2023-03-30 02:45 出处:网络
I want to create a Text file in my code behind file in my web application. Ho开发者_JAVA百科wever, I am not allowed to save this file to server. So I tried to use MemoryStream class to save my file in

I want to create a Text file in my code behind file in my web application. Ho开发者_JAVA百科wever, 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.


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.


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.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号