Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
30 lines (27 sloc) 1.05 KB
using System.IO;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack.Formats
{
public class CsvFormat : IPlugin
{
public void Register(IAppHost appHost)
{
//Register the 'text/csv' content-type and serializers (format is inferred from the last part of the content-type)
appHost.ContentTypes.Register(MimeTypes.Csv,
SerializeToStream, CsvSerializer.DeserializeFromStream);
//Add a response filter to add a 'Content-Disposition' header so browsers treat it natively as a .csv file
appHost.GlobalResponseFilters.Add((req, res, dto) =>
{
if (req.ResponseContentType == MimeTypes.Csv)
{
res.AddHeader(HttpHeaders.ContentDisposition, $"attachment;filename={req.OperationName}.csv");
}
});
}
public void SerializeToStream(IRequest requestContext, object request, Stream stream)
{
CsvSerializer.SerializeToStream(request, stream);
}
}
}