Atul Patil

Atul Patil

  • NA
  • 586
  • 73.7k

How download response data excel from browser using web api

May 16 2017 4:35 AM
I want to download excel or pdf from browser when service call will return result data, for reporting purpose.
Following is my code
 
public HttpResponseMessage GetCsv(List<CostCenter> searchEntity)
{
List<CostCenter> obj = new List<CostCenter>();
obj = searchEntity;
var sb = new StringBuilder();
sb.Append("CostCenterId,CostCenterName\r\n");
foreach (var record in obj)
{
sb.AppendFormat("=\"{0}\",", record.CostCenterId);
sb.AppendFormat("=\"{0}\",", record.CostCenterName);
}
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(sb.ToString());
result.Content.Headers.Add("Allow", "GET, PUT, POST, DELETE, HEAD");
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //attachment will force download
result.Content.Headers.ContentDisposition.FileName = "RecordExport.csv";
return result;
}
 
 
 
 

Answers (1)