3

A client will be issuing a GET request to our web api service, and we need to respond to that request with the specified file.

The file content will be as byte array like:

byte[] fileContent = Convert.FromBase64String(retrievedAnnotation.DocumentBody);

How do I respond to GET request with the above file content as a file?

I've stubbed out a controller:

[Route("Note({noteGuid:guid})/attachment", Name = "GetAttachment")]
[HttpGet]
public async Task<object> GetAttachment(Guid noteGuid)
{

    return new object();
}

Instead of the new object, how do I return the fileContent to the GET request?

0

1 Answer 1

4

You can return file content from web api using below method

public HttpResponseMessage GetAttachment(Guid noteGuid)
{
    byte[] content = Convert.FromBase64String(retrievedAnnotation.DocumentBody);
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new ByteArrayContent(content);
    response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = "fileName.txt";
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");

    return response;
}
Sign up to request clarification or add additional context in comments.

7 Comments

thanks so much, i've changed the question just a bit, im wondering how do i respond with the attachment WITHOUT saving it locally, i want to just stream that byte arraay
also, i'm not going to know the file type, im only going to know the extension of the fiel
i got this response: { "FileContents": "eWVz", "ContentType": "application/octet-stream", "FileDownloadName": "blabsssg4rrxt.txt" }
the filedownloadname is correct, however the filecontents should just be "yes"
didn't understand, can you please make me more clear ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.