简体   繁体   中英

Enrich FileStreamResult with metadata in ASP.NET Framework

I am generating an excel file. Then I am sending it back from my controller using the following code:

return new FileStreamResult(
    excelMemoryStream, 
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    {
        FileDownloadName = "data.xlsx"
    };

The FileStreamResult is from the System.Web.Mvc .

Now I have a business requirement to show a pop-up message on FE if there was no data added to the excel. There are still can be headers in the excel, so I can not just check the excel file size on FE. I need to send additional metadata along with the excel file.

I can not add another endpoint to check if there is any data going to be added to the excel. Such endpoint may result in a scenario when there is no data during the data presence check, then data gets added and then the endpoint to generate excel runs. All this results in a non empty excel being returned, but the empty excel message being shown on FE. So, there is a huge space for the race conditions.

So, my question is if it is possible to somehow enrich the System.Web.Mvc.FileStreamResult with more data in addition to the memory stream? Would be nice to be able to add an object of a custom type along with the memory stream.

The project uses <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> .

Here is a solution I found. Before returning the result I am adding the metadata header as well.

Response.Headers.Add("metadata", "{'any': 'data i want'}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM