I have this sample code from different sources and I want to try it first before applying it to my code which is pretty similar to the samples I got.
I have the following codes in my controller:
[Route("getcsv")]
[HttpGet]
public HttpResponseMessage GetCSV()
{
var data = new[]{ //Suppose you filter out these data
new{ Name="Ram", Email="ram@techbrij.com", Phone="111-222-3333" },
new{ Name="Shyam", Email="shyam@techbrij.com", Phone="159-222-1596" },
new{ Name="Mohan", Email="mohan@techbrij.com", Phone="456-222-4569" },
new{ Name="Sohan", Email="sohan@techbrij.com", Phone="789-456-3333" },
new{ Name="Karan", Email="karan@techbrij.com", Phone="111-222-1234" },
new{ Name="Brij", Email="brij@techbrij.com", Phone="111-222-3333" }
};
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(WriteTsv(data));
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;
}
public string WriteTsv<T>(IEnumerable<T> data)
{
StringBuilder output = new StringBuilder();
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in props)
{
output.Append(prop.DisplayName); // header
output.Append("\t");
}
output.AppendLine();
foreach (T item in data)
{
foreach (PropertyDescriptor prop in props)
{
output.Append(prop.Converter.ConvertToString(
prop.GetValue(item)));
output.Append("\t");
}
output.AppendLine();
}
return output.ToString();
}
I am calling this method through the following:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function save() {
window.open('https://localhost:44370/api/values/getcsv', '_blank', '');
}
</script>
</head>
<body>
<a class="btn btn-primary" onclick="save()">Export</a>
</body>
</html>
All of these give me the following result instead of downloading a file:
I don't have any idea what I am missing. Please help! Thank you!