如何在sapui5中的odata字符串中显示和下载从后端提取的前端文件



在SAP UI5中,我试图在前端显示任何格式的文件(pdf、word、jpg(,这些文件是使用odata从后端提取的。在前端,我已经在表中显示了数据-字段(文件名、扩展名、大小、附件链接(。我想要实现的是,当用户点击附件链接时,应该创建指定格式的文件并显示给用户。

您可以检查sap.ui.core.util.File保存方法。下面的工作示例。

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>

注:我之前也回答过同样的问题。我把我的答案抄在这儿。因为我不能将这个问题标记为与旧问题重复。

最新更新