StreamContent?

Topics: Web Api
Jun 13, 2011 at 9:23 PM

I have a resource that needs to return png files. Here is my test code:

public HttpResponseMessage Get(int designNumber) {
    var path = string.Format(CultureInfo.InvariantCulture, @"{0}\Images\Preview.png", AppDomain.CurrentDomain.BaseDirectory);
    using (var fileStream = new FileStream(path, FileMode.Open))
    {
        var memoryStream = new MemoryStream();
        fileStream.CopyTo(memoryStream);

        return new HttpResponseMessage {
            Content = new StreamContent(memoryStream)
        };
    }
}
If I access this through the browser, I get nothing? No errors, no content...nothing. Does StreamContent work? Am I doing something incredibly wrong here?

Jun 13, 2011 at 9:49 PM
Edited Jun 13, 2011 at 9:51 PM

Have you tried setting the HttpCode of the responsemessage? (to OK or something)

 

Edit: btw; try using a Formatter. You could just return an System.Drawing.Image object and have the formatter write it to the stream. You're method should also work though, I think.

Jun 13, 2011 at 11:52 PM

If you want to do this via a memory stream you will need to add,

memoryStream.Position = 0;

prior to passing it to StreamContent.

However, you should just be able to pass the filestream directly to the StreamContent constructor.

Jun 14, 2011 at 4:02 PM

I tried passing the file stream directly, and that didn't seam to work. Resetting the position is definitely a bug in my current code...ooops. I'll play around with a few things. I can plug in a formatter if I have to. Thanks everyone!

Nov 2, 2011 at 1:11 PM

Hi EisenbergEffect

did you ever manage to get it to work? I've tried doing the same as you, and don't get anything returned either, so I'd like to hear if you found a way to make it work...

 

TIA

Søren

Nov 2, 2011 at 2:07 PM

Hi Smolesen,

I have modified EisenbergEffect's original code a bit here in below example.

Please note: i have not tested the below code myself, but i am fairly confident that it should work.

public HttpResponseMessage Get(int designNumber) {
    var path = string.Format(CultureInfo.InvariantCulture, @"{0}\Images\Preview.png", AppDomain.CurrentDomain.BaseDirectory);
    
return new HttpResponseMessage {
            Content = new StreamContent(new FileStream(path, FileMode.Open))
        };
}

thanks,
Kiran Challa
Nov 2, 2011 at 3:08 PM
EisenbergEffect wrote:

I tried passing the file stream directly, and that didn't seam to work. Resetting the position is definitely a bug in my current code...ooops. I'll play around with a few things. I can plug in a formatter if I have to. Thanks everyone!

The reason passing file stream is not working in your case is because you are closing the Stream (because of the 'using' statement) before contents of it can be written onto the wire...instead you would need to do as i mentioned in my above post...the underlying layers would take care of closing your file stream once they have done their job...so you wouldn't need to worry about closing the stream explicitly...

thanks, Kiran Challa