-2

I am attempting to send files to a request by writing them to the response object. I currently have two ways. The first way (using FileStream) works and the second way(using MemoryStream) fails. I would like to know why the second way fails as I do not want to create a filestream all the time. Following is the code

    //This method does not work
    MemoryStream ms = MethodA(bundleStream);
    ms.CopyTo(Context.Response.Body);  //<---Copy memory stream to response (Fails - object at other end is empty)

The other method works. In this method I basically write to a file and then reopen that written file and copy to the 'Context.Response.Body'

//This method works.
MemoryStream ms = MethodB(bundleStream,"C:\\Mytestfile.exe"); //Wrtie to Mytestfile.exe 
ftest = new FileStream("C:\\Mytestfile.exe", FileMode.Open);  //Now open the same file that was written
ftest.CopyTo(Context.Response.Body);  <---Copy file stream to response (Works)
ftest.Close();

I wanted to know why the first method fails. Apparently when I read the incoming response its empty while I can easily read the response when the second method is used.

Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • You should be writing to the `Response.OutputStream`, not the body. – Igor Feb 23 '16 at 01:51
  • 1
    Please provide [MCVE] (because problem is likely in `MethodA` which returns non-readable/empty stream). Also basic search https://www.bing.com/search?q=c%23%20memorystream%20empty provides immediate answer http://stackoverflow.com/questions/6130469/memory-stream-is-empty - so you may clarify what did you try before asking the question. – Alexei Levenkov Feb 23 '16 at 02:06
  • @AlexeiLevenkov if you noticed that the answer had nothing to do with methodA or methodB. It had to do with seek. The marked answer indicates that. The reason i removed methodA and methodB was to make the question short and concise. A downvote is uncalled for. – Rajeshwar Feb 23 '16 at 02:22
  • Assuming code in `MethodA` properly creates new stream before returning to caller (i.e. `new MemoryStream(allBytesFromFile)` there is no way your current code can produce the error you are talking about. And you'd not need the "fix". Note that normally you should endup with "object disposed" exception if code in `MethodA` would at least follow good practice of disposing `IDisposable` objects... I really don't see how your claim that `MethodA` is not related to the question holds. – Alexei Levenkov Feb 23 '16 at 02:42

2 Answers2

3

You need to reset the position of the memory stream before copying it to the request stream.

ms.Seek(0, SeekOrigin.Begin);
andyroschy
  • 499
  • 3
  • 11
2

You should be using the OutputStream, not the body when writing binary data from a stream. I guess Body is an output stream although I can't find anything regarding property Body here HttpWebResponse, I did change the code based on what you had in your sample.

Here is a quick replacement for your first code part. Also you should dispose of the streams when you are done with them.

using(MemoryStream ms = MethodA(bundleStream)) // dispose of ms after you are done with it
{
  ms.Position = 0; // reset position
  ms.CopyTo(Context.Response.Body); // write to output stream (assuming .Body is a Stream)
  Context.Response.Body.Flush(); // flush response obj
}
Context.Response.End(); // end the response
Igor
  • 60,821
  • 10
  • 100
  • 175