从文件获取数据并写入文件

时间:2019-02-11 11:04:28

标签: angularjs

angularJS中是否可以将数据写入本地文件夹中的文件?我设法从文件中获取数据,现在我想写点东西。

我从data.json获取数据,但是order.txt404 (Not Found)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;
        });
    }
});  

1 个答案:

答案 0 :(得分:1)

您可以使用**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));

也请通过that

希望有帮助。 谢谢!