如何使用angular js读取url内容并将其写入文件 [英] how to read and write url content into a file using angular js
问题描述
通过使用此代码,我可以打开我想要的链接.但是我想读取该URL的特定内容,然后使用ANGULAR JS将其写入记事本.这是6我使用的代码.
by using this code i can able to open the link i want..but I want to read that particular content of the url and write it into the notepad using ANGULAR JS .. this is 6the code i used..
HTML和JAVASCRIPT
HTML and 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>
推荐答案
You could use a module like the angular-file-saver. It might be slightly involved - but the steps are outlined on the module's NPM page.
Basically, you need to install the angular-file-saver
module and then modify your myApp
module to inject it like this:
var app = angular.module('myApp', ['ngFileSaver']);
///您还需要更改控制器以包括FileSaver和Blob参数:
//You also need to change your controller to include the FileSaver and Blob params:
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');
});
};
}]);
我希望能对您有所帮助,请尝试一下,让我知道如何进行;
I hope that helps, give it a try and let me know how it goes;
这篇关于如何使用angular js读取url内容并将其写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!