Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an MVC4 WebAPI project and have a controller FileController that has this Get method in it:

public HttpResponseMessage Get(string id)
{
   if (String.IsNullOrEmpty(id))
       return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "File Name Not Specified");

    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

    var stream = fileService.GetFileStream(id);
    if (stream == null)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, "File Not Found");
    }

    response.Content = new StreamContent(stream);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

    response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = id;
    return response;            
}

In the browser going to localhost:9586/File/myfile.mp3it dispatches the file correctly as an attachment and you can save it. If it is an audio file, you can stream it from the HTML5 audio tag.

Now, I need to call this WebAPI method from an MVC4 web app, basically wrapping it. Here it comes:

public HttpResponseMessage DispatchFile(string id)
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:8493/");

    HttpResponseMessage response = client.GetAsync("api/File/"+id).Result;

    return response;
}

Going to localhost:8493/File/DispatchFile/my.mp3 returns:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cache Connection: Close Cache-Control: no-cache Date: Thu, 05 Sep 2013 15:33:23 GMT Server: ASP.NET Server: Development Server: Server/10.0.0.0 X-AspNet-Version: 4.0.30319 Content-Length: 13889 Content-Disposition: attachment; filename=horse.ogg Content-Type: application/octet-stream Expires: -1 }

So it looks like the Content is indeed StreamContent, but it doesn't return it as a saveable file. Now the question is, how to mirror the behavior when calling the API directly? Any suggestions much appreciated.

share|improve this question
 
I don`t got It! Do you want stream or provide a download? –  Fals Sep 16 at 21:54
add comment

1 Answer

I believe the use of HttpClient.Result is not the correct approach here. I think you may need to use the 'Content' property and then call ReadAsStreamAsync to get a handle on the file stream that is returned by your WebAPI method. At that point, you should be able to just write this stream to the response stream, allowing the file to be downloaded / audio to be streamed via HTML5.

See here for an example of using HttpClient to get a file (the link shows how to handle large files, but I believe the approach used there is what you will need to do):

http://developer.greenbutton.com/downloading-large-files-with-the-net-httpclient/

share|improve this answer
add comment

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.