7

I'm experimenting with a Web API service. I'm trying to do a file download via a GET request. The method fires just fine and hits my break point. I create a response and return it. Then, oddly, the break point get hits again. I'm using the Firefox add-on Poster to test it. Poster says there's no response from the server. Any idea why that is happening?

Here's the response creation code:

HttpResponseMessage result = this.Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = file.Length;
result.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddDays(-1));
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("Attachment") { FileName = file.Name };
return result;

The only significant change (I can think of) is to my WebApiConfig like so:

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });

My method signature looks like this: public HttpResponseMessage GetUpdate(int Id)

All my other actions are working fine. Am I missing something on the client side, like an accept header or something? I'm just doing a simple GET right now.

Thanks!

1
  • If I gut the method and just create an OK response and return it, it returns just fine. I have an Entity Framework using block and a FileStream using block in there. I'll start pulling code out and see if I can isolate the issue. Jun 5, 2013 at 21:08

1 Answer 1

6

Found it! The using statement seems to be the trouble. The stream is probably being disposed of before the result can get sent. I updated my code like so and it started working:

var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
result.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddDays(-1));
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("Attachment") { FileName = Path.GetFileName(filePath) };
8
  • 1
    btw, you need not set the ContentLength header explicitly.
    – Kiran
    Jun 5, 2013 at 21:39
  • 1
    StreamContent sets the content-length header for you. StreamContent checks the supplied stream to see if it can seek the stream and then tries to get the length of the stream.
    – Kiran
    Jun 6, 2013 at 14:42
  • 1
    I cannot tell from your answer exactly what you did different? I am dealing with a similar problem where FireFox is calling the action two times per request. Any additional info would be appreciated.
    – Paul
    Nov 28, 2013 at 5:47
  • 1
    Yeah, I didn't include the entire method. Sorry about that. What was happening is that I was using a 'using' block with my stream for the SteamContent object. So, the stream got closed and disposed of before the request completed correctly. I don't know exactly why the method appeared to fire twice, but skipping the using block on the stream fixed the issue for me. Nov 30, 2013 at 4:40
  • 2
    @DougDawson you should edit your answer to include the full method (pointing out that the using statements are remove) so incoming users like me don't have to dig into comments to find out why this answer worked for you. :) Mar 4, 2016 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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