How to get byte array content

Viewed 1579

I'm trying to write a webapi service that will return file contents. In the calling service, I can't seem to get the actual file contents, just the content header (in the ByteArrayContent)

I've tried to read the file and create the appropriate response message. When the calling service receives the http response and does a ReadAsByteArrayAsync() the only data I get is essentially the http response header.

    [HttpGet]
    [Route("{type}")]
    public async Task<HttpResponseMessage> GetFile(string type, string fileName)
    {
        try
        {
            var filePath = @"...\" + fileName;

            var stream = new MemoryStream();
            var fileStream = System.IO.File.OpenRead(filePath);
            stream.SetLength(fileStream.Length);
            await fileStream.ReadAsync(stream.GetBuffer(), 0, (int)fileStream.Length);
            stream.Position = 0;

            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(stream.ToArray())
            };
            response.Content.Headers.ContentLength = stream.Length;
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = fileName
            };
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            return response;
        }
        catch (KeyNotFoundException)
        {
            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }

This looks correct when I examine the response before it's sent:

{StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.ByteArrayContent, Headers:{ Content-Length: 222772 Content-Disposition: attachment; filename=blablablah.JPG Content-Type: application/octet-stream}}

When I look at the service side that called the webapi

var httpResponse = await httpClient.GetAsync(builder.ToString());
var image = await httpResponse.Content.ReadAsByteArrayAsync();

if (image != null && image.Length > 0)
{
    var x = Encoding.Default.GetString(image);

    return result;
}

the httpResponse is:

{StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Transfer-Encoding: chunked X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcRmlsZVN0cmVhbUFwaVxGaWxlU3RyZWFtQXBpXGFwaVxGaWxlXGV4cGVuc2VyZWNlaXB0?= Date: Fri, 11 Jan 2019 19:39:54 GMT Server: Kestrel X-Powered-By: ASP.NET Content-Type: application/json; charset=utf-8}}

and the actual content (from ReadAsByteArrayAsync()) is

"{\"version\":{\"major\":1,\"minor\":1,\"build\":-1,\"revision\":-1,\"majorRevision\":-1,\"minorRevision\":-1},\"content\":{\"headers\":[{\"key\":\"Content-Length\",\"value\":[\"222772\"]},{\"key\":\"Content-Disposition\",\"value\":[\"attachment; filename=47670af0-1916-4257-bbc2-b5f7da4c4839.JPG\"]},{\"key\":\"Content-Type\",\"value\":[\"application/octet-stream\"]}]},\"statusCode\":200,\"reasonPhrase\":\"OK\",\"headers\":[],\"requestMessage\":null,\"isSuccessStatusCode\":true}"

0 Answers
Related