Get Excel file from HttpResponseMessage

Viewed 7940

I am working on a ASP.NET Core 2.2 project and I need to download my Excel with my browser, but when I am doing my request, I just get some Json.

My Excel is in the stream, and the stream is not empty !

Here is my code :

HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
            var streamContent = new StreamContent(stream);

            streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            streamContent.Headers.ContentDisposition.FileName = "Excel.xlsx";

            message.Content = streamContent;

            return message;

And here is the response I get :

{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Type","value":["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]},{"key":"Content-Disposition","value":["attachment; filename=Excel.xlsx"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[],"requestMessage":null,"isSuccessStatusCode":true}

So, do someone know how I can send my Excel file with HttpResponseMessage ?

I can download my excel file using Filesteam, but I don't want to use that way because I can't get any Http error message (Bad request, Internal, etc.) But if someone know how I can send message like this and returning a Filestream, I will be glad to read your advice !

EDIT : Here is the code when I am returning a FileStreamResult

                return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
1 Answers
Related