I am trying to upload a file created using ClosedXML. I checked that the file is not corrupted, but for some reason it only works with Angular1, not Angular2. Web api code to return the file:
HttpResponseMessage response = new HttpResponseMessage(); response.StatusCode = HttpStatusCode.OK; response.Content = new ByteArrayContent(ms.GetBuffer()); response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); return response;
In Angular2, in my web service:
let headers = new Headers(); headers.append('Content-Type', 'application/json'); headers.append('responseType', 'arrayBuffer'); this.observableDataGet = this._http.post(`${AppSettings.REPORTS_API_URL}/Report/MonthlySpreadsheet`, {headers: this.getHeaders()}) .map(response => { if (response.status == 400) { return "FAILURE"; } else if (response.status == 200) { var contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; var blob = new Blob([response.arrayBuffer()], { type: contentType }); return blob; } })
and in my component:
.subscribe(blob => { var downloadUrl= URL.createObjectURL(blob); window.open(downloadUrl); },
The file loads, but it gets corrupted when I try to access it, and TWICE the file size when downloading using Angular1.
If I call the SAME API using Angular1, the file will download perfectly.
My service code:
function generateMonthlySpreadsheet(header) { var request = $http({ method: "post", responseType: 'arraybuffer', url: TEST_API_URL + 'Report/MonthlySpreadsheet', timeout: 30000, headers: header }); return ( request.then(handleSuccess, handleError) ); }
where handleSuccess returns response.data (which I cannot get for angular2)
and code to call the service:
alertAppService.generateMonthlySpreadsheet(header).then(function (data){ var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}); var objectUrl = URL.createObjectURL(blob); window.open(objectUrl);
Interestingly, in Angular2, if I just changed my web service to GET (then I wanted to POST, but just try), then he got rid of the service code and just made this call, the file is fine
window.open(`${AppSettings.REPORTS_API_URL}/Report/MonthlySpreadsheet`, "_blank");
So what is the difference? Why does the same or very similar code work for Angular1 but not Angular2 ??
- Karen