Angularjs download file via POST on IE8 / 9 without page refresh

I am trying to download a dynamically generated file from the server using a hidden form.

Below is the angular function that I use to submit a hidden form

$scope.downloadCsv = function() {
   var dataset = JSON.stringify($scope.dataset);
   var body = $('body');
   var reportParamJson = angular.toJson($scope.dataset);
   var hiddenForm = "<form action='/Reports/SaveTestCsv' method='POST' target='_blank'><input type='hidden' name='dataset' value='" + dataset + "'/ ><button id='submitCSV' type='submit'></button></form>";
   body.append(hiddenForm);
   $('#submitCSV').click();
}

Below is the .net mvc method to generate a response with a file

[HttpPost]
public ActionResult SaveTestCsv(string dataset)
{
    var data = JsonConvert.DeserializeObject<MyObject>(dataset);
    var binary = getTestCSV(data);
    var file = File(binary, "text/csv", "test.csv");
    return file;
}

Below is the corresponding html code from a partial html page that I include in the view using ng-include

<div><a href="#" ng-click="downloadCsv()">Download CSV</a></div>
<div ng-grid="gridOptions"></div>

.

When I click Download CSV, Chrome, FF and IE10 offer me to save the file without updating the page / view, but in IE8 / 9 the page is updated, so the content from the ng-include tag, in particular the html published above, is no longer present .

I am wondering if this has anything to do with hashban in the URL from IE8 / 9 and is there any way to fix this?

Edit

angular .net MVC , IE8

+4

Source: https://habr.com/ru/post/1544974/


All Articles