1

I am using

using (StreamWriter writer = new StreamWriter(Response.OutputStream, System.Text.Encoding.UTF8));

In order to directly write some lines of text and send them to the browser as an attachment.

I now though also want to save that text locally in a file, but Id rather avoid changing too much of my code. Can I write the contents of Response.OutputStream into a text file before ending the response?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
dearn44
  • 3,198
  • 4
  • 30
  • 63
  • How / what are you writing to the stream? You have not included any code that actually does the work. – mortb Feb 19 '16 at 09:51
  • @mortb that should not be relevatn. It's simply a streamWriter so I have a few `writer.Write()` commands that write some strings into it. – dearn44 Feb 19 '16 at 09:56
  • The code that exectues is *always* relevant. Most of us on SO is problem finders searching for patterns in the code.There is always more than one solution and the context the code is running in might contain the clues we need to help you. – mortb Feb 19 '16 at 10:40
  • Response.Filter might be the solution. https://msdn.microsoft.com/en-us/library/system.web.httpresponse.filter(v=vs.110).aspx If I'm correct you could create a filter (which is an implementation of a stream) that both saves the page and passes on the data unaltered. – mortb Feb 19 '16 at 10:54
  • I was not aware of this existing at all. So practically what you are suggesting is having a filter that will inherit my `StreamWriter` and use that to also save the stream in a file before ending my Response? – dearn44 Feb 19 '16 at 11:50
  • Basically yes, but inherit from `Stream`not `StreamWriter`. I must say though, that I have not yet tried this solution myself :) – mortb Feb 19 '16 at 12:06
  • Seems like a good solution, I will certainly give it a try and report back if I manage it. – dearn44 Feb 19 '16 at 12:20
  • Such a `Stream` is also implemented as part of the [`IHttpModule` solution](http://stackoverflow.com/a/1792864/1178314) I have linked in my answer. You could reuse that part for trying the filter solution. – Frédéric Feb 19 '16 at 12:20

2 Answers2

0

You can use the CopyTo method of the Stream object. At the end you can copy the whole OutputStream to an other one which write it to a file. (https://msdn.microsoft.com/en-us/library/dd782932(v=vs.110).aspx)

Kemy
  • 266
  • 6
  • 17
  • 1
    `Response.OuputStream` is not seekable as far as I know, your suggestion should lead to a `NotSupportedException`. – Frédéric Feb 19 '16 at 09:24
  • Frédéric is right. As MSDN states the CopyTo method can throw NotSupported exception is the current stream does not support reading. – Kemy Feb 19 '16 at 12:16
0

I believe what you ask for is not doable. I am quite sure Response.OutpuStream is not seekable (property CanSeek yielding false), meaning you won't be able to get at its start for reading its content. It is probably not readable either (property CanRead yielding false).

Attempting any of those operations would yield a NotSupportedException.

If your needs are for some basic logging, you may work around that by enabling .Net standard network traces. Or code an IHttpModule as suggested here.

Otherwise, you may use an intermediate MemoryStream with your StreamWriter, then reset this MemoryStream to Position 0, write it to OutputStream, reset it again to Position 0, write it to your file.

Community
  • 1
  • 1
Frédéric
  • 9,364
  • 3
  • 62
  • 112
  • In the end using a MemoryStream seemed like a good and fast solution so I ended up following your advise. – dearn44 Feb 19 '16 at 10:48