最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Download json using Angularjs - Stack Overflow

programmeradmin4浏览0评论

I have webapi returning me data and that data i need to download in a txt file. I have done it using below code and i am getting result as desired. I am somehow looking for a better approach .Is there any angular way to do this ?

$scope.downloadResponseAsInfoFile = function (response) {
    var link = document.createElement("a");
    link.download = "info.txt";
    var data = "text/json;charset=utf-8," + encodeURIComponent(response);
    link.href = "data:" + data;
    link.click();
};

how can i handle exception in case i dont get response from server.

I have webapi returning me data and that data i need to download in a txt file. I have done it using below code and i am getting result as desired. I am somehow looking for a better approach .Is there any angular way to do this ?

$scope.downloadResponseAsInfoFile = function (response) {
    var link = document.createElement("a");
    link.download = "info.txt";
    var data = "text/json;charset=utf-8," + encodeURIComponent(response);
    link.href = "data:" + data;
    link.click();
};

how can i handle exception in case i dont get response from server.

Share Improve this question asked Dec 5, 2016 at 4:35 pankajpankaj 1,0605 gold badges22 silver badges42 bronze badges 3
  • What calls your function? Where does response e from? Presumably you'd handle HTTP errors there – Phil Commented Dec 5, 2016 at 4:37
  • write if condition based on your response – Sujithrao Commented Dec 5, 2016 at 4:42
  • @Phil its like this var response = myService.downloaInfo(id, name); if (response != null && response != "") { $scope.downloadResponseAsInfoFile (response) } – pankaj Commented Dec 5, 2016 at 6:16
Add a ment  | 

3 Answers 3

Reset to default 5

In downloadResponseAsInfoFile, if response is ing through http call than you need to handle exception according to response.

$scope.downloadResponseAsInfoFile = function (response) {
    // if this response is ing through http call than make condition according to http response.statusCode
    //check response is undefined, null or empty
    if(typeof response == 'undefined' || response == null || response == "")
        return ;

    var link = document.createElement("a");
    link.download = "info.txt";
    var data = "text/json;charset=utf-8," + encodeURIComponent(response);
    link.href = "data:" + data;
    link.click();
};

Angular Way: article

You can use angular's $http provider for fetching data from the web api See documentation here : - https://docs.angularjs/api/ng/service/$http

$http.get(url).then(function (response) {
var link = document.createElement("a");
link.download = "info.txt";
var data = "text/json;charset=utf-8," + encodeURIComponent(response);
link.href = "data:" + data;
link.click();
};

You could try using native JavaScript API - Blob and FileSaver.js saveAs No need to deal with any HTML elements at all -

var data = {
    key: 'value'
};
var fileName = 'myData.json';

// Create a blob of the data
var fileToSave = new Blob([JSON.stringify(data)], {
    type: 'application/json',
    name: fileName
});

// Save the file
saveAs(fileToSave, fileName);

Just paste the above code in your controller's callback.

发布评论

评论列表(0)

  1. 暂无评论