I am creating an Excel file in my WebAPI. I βstoreβ it in the storage device, and then I will answer in the following way:
var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(ms) }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = projectName + ".xlsx" };
It looks like the server side patch is working. If I write this memystream to filestream, the file is created and can be opened without any problems.
On the angular side, how can I recreate a file when a button is clicked?
I tried something like this:
$scope.exportQuotas = function (projectName) { homeService.GetQuotas(projectName, $routeParams.token, $scope.selection).then( function (data) { var dataUrl = 'data:application/octet-stream;' + data var link = document.createElement('a'); angular.element(link) .attr('href', dataUrl) .attr('download', "bl.xlsx") .attr('target', '_blank') link.click(); }) }
The file was created, but when I tried to open it, it was corrupted ... I tried to change the data type to vnd.ms-excel in angular, but it did not work ... How can I load the file when clicked?
EDIT After Jorg's answer, I tried the following: Return api:
Status Code: 200 Pragma: no-cache Date: Tue, 02 Sep 2014 02:00:24 GMT Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Type: application/binary Access-Control-Allow-Origin: * Cache-Control: no-cache X-SourceFiles: =?UTF-8?B? QzpcVXNlcnNcdHJpaGFuaC5waGFtXFByb2plY3RzXFF1b3RhUXVlcnlcUXVvdGFRdWVyeUFQSVxhcGlccXVvdGFcR2V0?= Content-Disposition: attachment; filename=O14Y0129AUG.xlsx Content-Length: 13347 Expires: -1
From what I see, it looks right.
On the client side:
var file = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' }); var fileURL = URL.createObjectURL(file); window.open(fileURL);
An excel file is being created, but it is still corrupted ...
thanks
angularjs c # rest excel asp.net-web-api
user2522221
source share