How to open a MemoryStream file in a browser?

I want to create a text file in my code behind the file in my web application. However, I am not allowed to save this file on the server. So I tried to use the MemoryStream class to save my file in memory. Still me

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

It seems to work, but my requirement is to open this file in the client’s browser when it clicks the button. Since this file does not have a physical path, for example .... /text.txt. I do not know how to open it in a browser.

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

Thanks in advance.

+4
source share
4 answers

It is much easier than you think. Keep in mind that the HTTP protocol does not actually transfer “files” in the strict sense of the word. It conveys requests and responses, each of which contains headers and content. In this case, you are concerned about the headers and content of the response.

The easiest way to do this in a WebForms application is through a generic handler . In particular, look at the implementation of the handler response in this link:

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

This writes the contents of the image file in response after the response header is configured correctly. What you want is closer to what was commented on in the implementation:

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

This will send plain text to the browser, nothing more. The browser does not believe that this is a web page or something like that, will not apply any styles to it, etc. As for the web browser, it just uploaded a text file with the words "Hello World" in it, you can Response.Write() all the text you want to create for this file.

You can also customize response headers to provide the browser with even more information. For example, if you added the following header to the HttpResponse :

 Content-Disposition: attachment; filename=myfile.txt 

The browser will then translate this to mean that this “file” must be downloaded and saved, and not just displayed. (Of course, the user's browser settings may say that he will still display it, but this is the right way for the server to “suggest” the browser to save the file.)

From the point of view of the browser, it does not matter where the “file” came from. Regardless of whether it was from the server's file system or dynamically generated or magically conjured, it does not matter. The browser only deals with response headers and content. If the headings say this is text, and say it is a file, then the content will be treated as a text file.

+5
source

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

 Response.WriteLine("Something"); 

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

+3
source

I sincerely believe that this is not a good model in website development.

It is just reading your file and sending its data in the form of text on the client side (web browser), editing it in a text field, sending back the changed text and saving it as a file on the path or storage of your choice.

HTTP is a stateless protocol, so you won’t leave the file open on the server side while its contents are being edited on the client side, since both levels are completely disabled after the server response is complete.

+2
source

Well, I think I understand what you want. You say that you have a button with which you want to go to the contents of the text file that you want to create in memory, but you don’t know which url the browser will send when the user clicks the button

If so, then what can you 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 fact is that you have to split into two different files (or a separate action based on the query string).

+1
source

All Articles