Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working on a web service using ASP.NET MVC's new WebAPI that will serve up binary files, mostly .cab and .exe files.

The following controller method seems to work, meaning that it returns a file, but it's setting the content type to application/json:

public HttpResponseMessage<Stream> Post(string version, string environment, string filetype)
{
    var path = @"C:\Temp\test.exe";
    var stream = new FileStream(path, FileMode.Open);
    return new HttpResponseMessage<Stream>(stream, new MediaTypeHeaderValue("application/octet-stream"));
}

Is there a better way to do this?

share|improve this question

3 Answers

up vote 69 down vote accepted

Try using a simple HttpResponseMessage, with a StreamContent inside, which should work fine.

public HttpResponseMessage Post(string version, string environment,
    string filetype)
{
    var path = @"C:\Temp\test.exe";
    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    var stream = new FileStream(path, FileMode.Open);
    result.Content = new StreamContent(stream);
    result.Content.Headers.ContentType = 
        new MediaTypeHeaderValue("application/octet-stream");
    return result;
}
share|improve this answer
Nice, Carlos. That worked! – Josh Earl Mar 4 '12 at 13:48
14  
Would you happen to know when the stream gets closed? I am assuming the framework ultimately calls HttpResponseMessage.Dispose(), which in turn calls HttpResponseMessage.Content.Dispose() effectively closing the stream. – Steve Guidi Apr 20 '12 at 18:36
9  
Steve - you're correct and I verified by adding a breakpoint to FileStream.Dispose and running this code. The framework calls HttpResponseMessage.Dispose, which calls StreamContent.Dispose, which calls FileStream.Dispose. – Dan Aug 22 '12 at 20:02
@carlosfigueira Would you mind putting these using comments in your answer? – sennett Jun 19 at 3:27
2  
You can't really add a using to either the result (HttpResponseMessage) or the stream itself, since they'll still be used outside the method. As @Dan mentioned, they're disposed by the framework after it's done sending the response to the client. – carlosfigueira Jun 19 at 16:18
show 1 more comment

The overload that you're using sets the enumeration of serialization formatters. You need to specify the content type explicitly like:

httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
share|improve this answer
Thanks for the reply. I tried this out, and I'm still seeing Content Type: application/json in Fiddler. The Content Type appears to be set correctly if I break before returning the httpResponseMessage response. Any more ideas? – Josh Earl Mar 3 '12 at 14:34

You could try

httpResponseMessage.Content.Headers.Add("Content-Type", "application/octet-stream");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.