Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Open MemoryStream File In Browser?

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.

like image 832
emre nevayeshirazi Avatar asked Aug 24 '11 10:08

emre nevayeshirazi


3 Answers

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.

like image 94
David Avatar answered Sep 28 '22 04:09

David


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.

like image 39
Tim Rogers Avatar answered Sep 28 '22 04:09

Tim Rogers


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.

like image 40
Matías Fidemraizer Avatar answered Sep 28 '22 03:09

Matías Fidemraizer