1

Currently I have all ajax request that send Authentication token, but I created a service to export to excel, ajax doesn't support that and I am using location.href for the get request. Is there any way to add that token?

 var getUrl = '/LandingView/ExportToExcel?excelHeader=' + excelHeader;
                        getUrl += '&companyId=' + $scope.options.companyId;
                        getUrl += '&startDate=' + $scope.options.startDate.toJSON();
                        getUrl += '&endDate=' + $scope.options.endDate.toJSON();
                        getUrl += '&page=' + $scope.options.page;
                        getUrl += '&stateFilter=' + $scope.options.stateFilter;
                        getUrl += '&rows=' + $scope.options.rows;
                        getUrl += '&alertFilter=' + $scope.options.alertFilter;
                        getUrl += '&mode=' + $scope.options.mode;
                        getUrl += '&showLanguages=' + $scope.options.showLanguages;

 location.href = getUrl;

When I do this I need to include in the header the bearer token.

PS: I am using ASP.NET MVC in the backend and angularJS in the frontend.

Dayán Ruiz
  • 611
  • 1
  • 9
  • 22
  • Can you keep your ajax request to generate the Excel file and then generate and click a download link as described [here](https://stackoverflow.com/questions/30776593/save-ajax-response-to-file)? – ejohnson Mar 26 '18 at 22:04
  • [this may help](https://stackoverflow.com/a/26373493/5621827) – jitender Mar 27 '18 at 04:07
  • When I do the ajax request the download is not working, I made a search and says that it is impossible with ajax, and about the link you just wrote, I can't see how to create that request, I need an example please! @jitender – Dayán Ruiz Mar 28 '18 at 15:28
  • @ejohnson I search what you wrote, but what I do is create a request, then a backend ASP.NET MVC method process the request, that method returns a byte array with type and file name. I do not know how to adapt your answer to my situation. – Dayán Ruiz Mar 28 '18 at 15:31
  • you can find some examples [here](https://stackoverflow.com/q/4545311/5621827) – jitender Mar 29 '18 at 04:02

1 Answers1

0

I found an article that helped my for good. I will set the link over here.

The idea of this approach is that you can do with ajax, then return the file name saved in temp folder with a guid generated name, and with that file name download the file with location.href, finally delete the file.

https://www.codeproject.com/Tips/1156485/How-to-Create-and-Download-File-with-Ajax-in-ASP-N

Dayán Ruiz
  • 611
  • 1
  • 9
  • 22