1

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?

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.