I want to download excel or pdf from browser when service call will return result data, for reporting purpose.
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;
}