简体   繁体   English

如何在Angular表中下载xml数据

[英]How to download xml data in Angular table

I am showing list of data in a table. 我在表中显示数据列表。 In one column I am providing an option to download xml data for every row. 在一个专栏中,我提供了一种为每一行下载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>"
        }

So If i click on download button it should downlaod Obj.requestdata of corrsponding row. 因此,如果我单击下载按钮,则应该下载对应行的Obj.requestdata。 How to achieve this? 如何实现呢?

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

If your users use modern browsers you can use the Blob object as follow : 如果您的用户使用现代浏览器,则可以按以下方式使用Blob对象

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM