In asp.net web api, how to create a functionality of download a sample .xlsx file? I tried the below function.
public HttpResponseMessage DownloadExcelFile()
{
string filePath = Directory.GetCurrentDirectory() + "/Modules/SampleFiles/sample3.xlsx";
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(new FileStream(filePath, FileMode.Open));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "sample3.xlsx"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
return response;
}
I tried the below
public HttpResponseMessage DownloadExcelFile()
{
string filePath = Directory.GetCurrentDirectory() + "/Modules/SampleFiles/sample3.xlsx";
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(new FileStream(filePath, FileMode.Open));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "sample3.xlsx"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
return response;
}
Have you tried using the MediaType below?
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");