如何在Angular表中下载xml数据

时间:2016-03-01 11:42:54

标签: javascript angularjs xml angularjs-scope ngtable

我正在显示表格中的数据列表。在一列中,我提供了为每行下载xml数据的选项。

       0{
        firstName: null
        id: "04674"
        lastName: null
        orderId: "TEM001"
        productId: "TEM"
        receiptPeriod: "2016-02-26"
        requestData: "<edem:eD..........d>"
        }
      1{
        firstName: null
        id: "044a18b1022f674"
         lastName: null
         orderId: "TEM001"
        productId: "SURE"
        receiptPeriod: "2016-02-26"
        requestData: "<edem:eD..........d>"
        }

所以如果我点击下载按钮,它应该下载相应行的Obj.requestdata。怎么做到这一点?

      <td class="align-center">
         <span class="glyphicon glyphicon-download-alt btn-blue"ng-   click='something' ></span>
      </td> 

1 个答案:

答案 0 :(得分:0)

如果您的用户使用现代浏览器,您可以使用Blob object,如下所示:

&#13;
&#13;
var app = angular.module('App', []);

app.controller('AppCtrl', [ "$scope", function($scope) {
    $scope.docs = [
      { title: "Titi", xml: "<a><b>Titi</b><c>1</c></a>"},
      { title: "Tata", xml: "<a><b>Tata</b><c>2</c></a>"},
      { title: "Toto", xml: "<a><b>Toto</b><c>3</c></a>"}
    ];
  
    // OPTION 1
    $scope.exportAsPopup = function(doc) {
      // blob text should be in an array
      var fileToExport = new Blob([doc.xml], {type: "text/xml", name:"export_"+doc.title+".xml"});
      // should work, but iframe policy don't allow it on StackOverflow
      window.open(URL.createObjectURL(fileToExport));
    }
    
    // OPTION 2
    $scope.exportAsLink = function(doc) {
      // blob text should be in an array
      var fileToExport = new Blob([doc.xml], {type: "text/xml"});
      
      var a = document.createElement("a");
          a.href = URL.createObjectURL(fileToExport);
          a.target = "_blank";
          a.download = "export_"+doc.title+".xml";
          a.click();
    }
    
  }])
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<table ng-app="App" ng-controller="AppCtrl">
  <tr ng-repeat="doc in docs">
    <td>
      {{doc.title}} 
      <span class="export" ng-click="exportAsPopup(doc)">export via popup</span> | 
      <span class="export" ng-click="exportAsLink(doc)">export via link</span></td>
  </tr>
</table>
&#13;
&#13;
&#13;