Reputation: 9013
I need WebAPI method, which returns file and some additional data. I try to do it:
HttpResponseMessage result = Request.CreateResponse<OCRResult>(HttpStatusCode.OK, ocrResult);
var stream = new FileStream(@"D:\\_forTest.jpg", FileMode.Open);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = Path.GetFileName("_forTest.jpg");
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
return result;
but I see only file in returned result, not additional json data (of course, content type is not json). How to "combine" it?
Upvotes: 1
Views: 1851
Reputation: 39007
If you look at the documentation of the Request.CreateResponse<T>
method: https://msdn.microsoft.com/en-us/library/hh969056(v=vs.118).aspx
value: The content of the HTTP response message.
So basically, you put ocrResult
as the content of your message, then overwrite it right after:
result.Content = new StreamContent(stream);
In any case, you can't send a file and display content at the same time in an HTTP response. This is because both are actually sent the same way (and the content-type tells the browser whether it is displayable content or a file). That's why most websites display a content page with an automatic redirection, and the message "your download will start in a few seconds".
Upvotes: 2