TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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;
}
Reply
Answers (
1
)
How the format of web service response (.asmx) should be?
Difference Between API and Web Service