2

Is there a way in angularJS to write data to file in local folder? I managed to get data from the file and now i want to write something.

I get data from data.json, but order.txt is 404 (Not Found) or ERR_CONNECTION_REFUSED.

angular.module("sportsStore")
.constant("dataUrl", "data.json")
.constant("orderUrl", "order.txt")
.controller("sportsStoreCtrl", function ($scope, $http, $location,
    dataUrl, orderUrl, cart) {

    $scope.data = {
    };

    $http.get(dataUrl)
        .success(function (data) {
            $scope.data.products = data;
        })
        .error(function (error) {
            $scope.data.error = error;
        });

    $scope.sendOrder = function (shippingDetails) {
        var order = angular.copy(shippingDetails)
        order.products = cart.getProducts();

        $http.post(orderUrl, order)
        .success(function (data) {
            $scope.data.orderId = data.id;
        })
        .error(function (error) {
            $scope.data.orderError = error;
        });
    }
});  
bszyd
  • 27
  • 6
  • I think this is not possible. Some alternatives suggested here: https://stackoverflow.com/questions/30288087/is-it-possible-to-write-data-to-a-locally-json-file-with-nothing-but-angular – Mebin Joe Feb 11 '19 at 11:18
  • 1
    That makes sens. Thanks for the link. – bszyd Feb 11 '19 at 11:24

1 Answers1

1

You can try it by using **blob**

$scope.data = {
    };

$scope.writeFile = function () {
    $scope.data = angular.toJson($filter('filter')($scope.sendOrder));
};
}]);

var blob = new Blob([scope.writeFile],{
type: "application/json;charset=utf-8;"
});
downloadLink.attr('href', window.URL.createObjectURL(blob));

Kindly go through that too.

Hope it helps. Thanks!

Avinash Singh
  • 4,970
  • 8
  • 20
  • 35