Sorry if this is a duplicate, I looked around the forums but nothing seemed to be of help.
I’m trying to return an excel stream from web api. And the resulting excel file opens with “We found a problem with some content in ‘export.xlsx’. Do you want us to try to recover…”. And clicking yes to that gives a “File is corrupted and cannot be opened”.
MemoryStream mstream = new MemoryStream();
workbook.save(mstream, SaveFormat.Xlsx);
mstream.Seek(0, SeekOrigin.Begin);
HttpResponseMessage res = new HttpResponseMessage(System.Net.HttpStatusCode.Ok);
res.Content = new ByteArrayContent(mstream.ToArray());
res.Content.Headers.ContentLength = mstream.Length;
res.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "export.xlsx"
}
res.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.openxmlformats");
return res;
And if it matters, this is how I’m handling it in JS:
api.getExport(...).then(res => {
const blob = new Blob([res.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8" });
});
Does anything look off to you guys? Any help would be greatly appreciated.
Thanks.