I am working on a project with Web API on the server side and angularjs on the client side. One of the requirement is to retrieve data from database based on some parameters and export it in csv as well as pdf formats. We are using async/await pattern. I am kind of new to Web API and angularjs and did not find a good resource in this direction. Can anybody suggest anything that will help me getting started in the right direction ?
So far this is what I did but not succesful in getting the desired output.
// This is WEB API method
[HttpGet]
[Authorize]
[Route("ExportAppData")]
public IHttpActionResult ExportAppData()
{
List<App> apps = _appRepository.GetAllApps();
string csvData = ConvertToCSV(apps);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(csvData);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "file.csv";
return Ok(result);
}
// This is angularjs function call to call the above method
function exportAppData() {
return $http(
{url: 'api/Apps/ExportAppData',
method: "GET",
headers: {'Content-Type': 'application/octet-stream'}
}
);
}
When I run I am getting an error message "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource."
Any idea where I am missing something ?