Reputation: 57
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");
Upvotes: 3
Views: 9707
Reputation: 62260
You could read as Byte array from Stream, and return FileContentResult.
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace DemoWebCore.Controllers
{
[Route("api/[controller]")]
public class FilesController : Controller
{
// GET api/files/sample.png
[HttpGet("{fileName}")]
public async Task<ActionResult> Get(string fileName)
{
var cd = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline")
{
FileName = "Excel.xlsx"
};
Response.Headers.Add("Content-Disposition", cd.ToString());
StreamContent stream = YOUR_STREAM_SOURCE
byte[] content = await stream.ReadAsByteArrayAsync();
return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
}
Upvotes: 5