2016-12-05 3 views
2

Ich habe Webapi gibt mir Daten zurück und diese Daten muss ich in eine TXT-Datei herunterladen. Ich habe es unter Verwendung von unten Code gemacht und ich bekomme Ergebnis wie gewünscht. Ich bin irgendwie auf der Suche nach einem besseren Ansatz. Gibt es einen eckigen Weg, dies zu tun?Download Json mit Angularjs

$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(); 
}; 

Wie kann ich Ausnahme behandeln, falls ich keine Antwort vom Server bekomme.

+0

Was nennt Ihre Funktion? Woher kommt "Antwort"? Vermutlich würden Sie HTTP-Fehler dort behandeln – Phil

+0

schreiben Sie, wenn Bedingung basierend auf Ihrer Antwort – Sujithrao

+0

@Phil ist es wie diese Antwort = myService.downloaInfo (ID, Name); wenn { $ scope.downloadResponseAsInfoFile (Antwort) } – pankaj

Antwort

4

In downloadResponseAsInfoFile, wenn die Antwort über HTTP-Aufruf kommt, müssen Sie die Ausnahme gemäß response behandeln.

$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

+0

wirkt wie ein Zauber in Chrome –

0

können Sie Winkel des verwenden $ http-Provider für Daten aus dem Web api Siehe Dokumentation holen sich hier: - 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(); 
}; 
+0

leichten zusätzlich zu dem obigen Code (Antwort = null && response = ""!): - $ http.get (url). dann (Funktion successCallback (Antwort) { // Ihr Code }, Funktion errorCallback (Antwort) { // errmsg }); – Vibhu

+0

Im Allgemeinen sind Antworten viel hilfreicher, wenn sie eine Erklärung enthalten, was der Code beabsichtigt ist, und warum das das Problem löst, ohne andere einzuführen –

+0

@Odera danke für den Tipp, ich habe entsprechend zusätzliche Informationen zur Verfügung gestellt, um die Antwort hilfreich zu machen . – Vibhu

0

Sie könnten versuchen, mit nativen JavaScript API - Blob und FileSaver.js saveAs Keine Notwendigkeit, mit HTML-Elementen überhaupt zu arbeiten -

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); 

Fügen Sie einfach den obigen Code in den Rückruf Ihres Controllers ein.