3

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.

3
  • What calls your function? Where does response come from? Presumably you'd handle HTTP errors there Commented Dec 5, 2016 at 4:37
  • write if condition based on your response Commented Dec 5, 2016 at 4:42
  • @Phil its like this var response = myService.downloaInfo(id, name); if (response != null && response != "") { $scope.downloadResponseAsInfoFile (response) } Commented Dec 5, 2016 at 6:16

3 Answers 3

5

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

$scope.downloadResponseAsInfoFile = function (response) {
    // if this response is coming 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

Sign up to request clarification or add additional context in comments.

Comments

1

You can use angular's $http provider for fetching data from the web api See documentation here : - https://docs.angularjs.org/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();
};

3 Comments

slight addition to the above code:- $http.get(url).then(function successCallback(response) { //yourcode }, function errorCallback(response) { //errmsg });
Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others
@Odera thanks for the tip , I have appropriately provided additional info to make the answer helpful.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.