如何使用angular js将url内容读入和写入文件

时间:2016-11-01 07:14:59

标签: javascript angularjs

通过使用此代码,我可以打开我想要的链接..但我想读取该网址的特定内容,并使用ANGULAR JS将其写入记事本。 这是我使用的代码..

HTML和JAVASCRIPT

   <!DOCTYPE html>
   <html ng-app="myApp" ng-controller="myCtrl">
   <head>
    <script>
    function start_scroll_down() { 
     scroll = setInterval(function()
     { window.scrollBy(0, 1000);           
      console.log('start');}, 1500);
     }

     function stop_scroll_down() {
      clearInterval(scroll);
     console.log('stop');
     }
   </script>
     <button onclick="start_scroll_down();">Start Scroll</button>
    <button onclick="stop_scroll_down();">Stop Scroll</button>
    <script src="https://ajax.googleapis.com
     /ajax/libs/angularjs/1.1.5/angular.min.js"></script>
    <script>
    var app = angular.module('myApp', ['ngFileSaver']);

    app.controller('myCtrl', ['$http','FileSaver', 
     'Blob', function($http, FileSaver, Blob){
    $scope.foo = function() {
     $http.get('http://www.thehindu.com/news/').
           success(function(pageContent){               
       var data = new Blob([pageContent],
       {type: 'text/plain;charset=utf-8' });
     FileSaver.saveAs(data, 'text.txt');            

   });

   };  
      }]);
  </script>
    <meta charset=utf-8 />
     <title>JS Bin</title>
    </head>
      <body>
    <a href="http://www.thehindu.com/news/" target="_blank">News</a>
    <button ng-click="foo()">News</button>
    </body>
     </html>

1 个答案:

答案 0 :(得分:0)

您可以使用angular-file-saver之类的模块。它可能涉及一些 - 但步骤在模块的NPM页面上列出。 基本上,您需要安装angular-file-saver模块,然后修改myApp模块以将其注入:

var app = angular.module('myApp', ['ngFileSaver']);

//您还需要更改控制器以包含FileSaver和Blob参数:

app.controller('myCtrl', ['$http','FileSaver', 'Blob', function($http, FileSaver, Blob){
   //now you can use the file save inside your $scope.foo like this:
      $scope.foo = function() {
        //get content of the remote URL (take care of the CORs)             
         $http.get('http://www.thehindu.com/news/').success(function (pageContent){               
          //here you create file and save content to it
           var data = new Blob([pageContent], {type:   'text/plain;charset=utf-8' });
        FileSaver.saveAs(data, 'text.txt');            

      });

      };  
}]);

我希望有所帮助,试一试,让我知道它是怎么回事;