0

So I have written a function to pull data from a C# controller. It is pulled through into a FormData object and then converted into an array, the array is supposed to be broken down into comma separated values.

however when I download the csv this is what I get (screenshot):

http://postimg.org/image/wex20t1h3/

Here is my Angularjs controller code:

angular.module("umbraco")
.controller("ExportAllController", function ($scope, $http) {

    $scope.filedownload = {};
    $scope.exportAll = function () {
        var downloadUrl = " /umbraco/backoffice/api/ExportDictionaryAll/ExportAll";
        var fd = new FormData();

        fd.append('file', $scope.filedownload);

        $http.get(downloadUrl, fd)
        .success(function (fd) {
            // ok
            $scope.fd = fd.split(',');

            console.log($scope.fd);
            alasql('SELECT * INTO CSV("AllDictionaryItems.csv" ) FROM ?', [$scope.fd]);
        })
        .error(function () {
            // handle upload error
            alert("Download Unsuccessful!");
        });

    };
});
  • 1
    Why are you not simpling opening a new window with url /umbraco/backoffice/api/ExportDictionaryAll/ExportAll with the correct return type / mime type headers set on the controller? – Eyescream Apr 22 '16 at 13:08
  • I've been an idiot the whole time! All I needed to do was use a window.open(downloadUrl, "_blank"); in my angular code and it works. Now I don't even need the alasql nonsense. – Jonathan Yaniv Ben Avraham Apr 22 '16 at 13:20
  • 1
    glad that it worked :) – Eyescream Apr 23 '16 at 09:58

1 Answers1

0
angular.module("umbraco")
.controller("ExportAllController", function ($scope, $http) {

    $scope.filedownload = {};
    $scope.exportAll = function () {
        var downloadUrl = " /umbraco/backoffice/api/ExportDictionaryAll/ExportAll";
        var fd = new FormData();

        fd.append('file', $scope.filedownload);
        window.open(downloadUrl, "_blank");
        $http.get(downloadUrl, fd)
        .success(function (fd) {
            // ok
            alert("Download Successful!");
        })
        .error(function () {
            // handle upload error
            alert("Download Unsuccessful!");
        });

    };

    $scope.greetings = [{
        name: "Export All Dictionary Items to CSV",
       def: "Click the button below to export a CSV file with all current dictionary items."
    }]
});

I was missing a window.open(downloadUrl, "_blank");