Displaying csv file from server in sapui5

◇◆丶佛笑我妖孽 提交于 2019-12-13 09:21:01

问题


I have a server that is returning a csv file. I need to allow this file to be downloaded to the client side using sapui5. I am fairly new to it so I am having trouble handling the response. Any ideas would be much appreciated. Thanks in advance


回答1:


You can check sap.ui.core.util.File save method. Working example below.

sap.ui.define([
	"sap/ui/core/util/File"])
 sap.ui.getCore().attachInit(function() {
   "use strict";
   sap.ui.controller("MyController", {
     onInit: function() {
     
     }
   });
   sap.ui.xmlview({
     viewContent: jQuery("#myView").html()
   	}).placeAt("content");
 	 });
   
 doDownload = function(oEvent) {
   sap.ui.core.util.File.save("csv content", "myfile", "csv", "text/csv");
 }
<!DOCTYPE html>
<title>SAPUI5</title>
<script src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js" id="sap-ui-bootstrap" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.m" data-sap-ui-bindingSyntax="complex" data-sap-ui-compatVersion="edge" data-sap-ui-preload="async"></script>

<script id="myView" type="ui5/xmlview">
  <mvc:View controllerName="MyController" xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
  xmlns:layout="sap.ui.commons.layout">

    <layout:MatrixLayout>
    	<layout:rows>
      	<layout:MatrixLayoutRow>
        	<layout:MatrixLayoutCell padding="None">

                <Button text="Download CSV" press="doDownload" />

        	</layout:MatrixLayoutCell>
        </layout:MatrixLayoutRow>
      </layout:rows>
    </layout:MatrixLayout>


  </mvc:View>
</script>

<body class="sapUiBody">
  <div id="content"></div>
</body>


来源:https://stackoverflow.com/questions/58659402/displaying-csv-file-from-server-in-sapui5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!