我需要使用SSJS创建一个文本文件。我从文档中获取数据然后我将在文本文件中写入所有数据。我尝试了一些解决方案,我发现在互联网上搜索但我无法得到我需要的结果。
关心
任何建议表示赞赏
Cumhur Ata
更新:
writer.endDocument();
facesContext.responseComplete();
writer.close();
更新2:
<xp:button value="Create TXT File" id="button1">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:var con = facesContext.getExternalContext();
var response:com.ibm.xsp.webapp.XspHttpServletResponse = con.getResponse();
response.setContentType("text/plain; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
response.setHeader("Content-Disposition", "attachment; filename=\"file.txt\"" );
var strEntr = "\n";
var str1 = "TXTV2[] \n";
str1 += "NUMBER|CLIENT_ID|START_DATE|END_DATE|\n";
str1 += "UNITS|DESCRIPTION|CLASSIFICATION|"+strEntr;
str1 += "[]";
response.getOutputStream().write(str1.getBytes());
facesContext.responseComplete(); }]]></xp:this.action>
</xp:eventHandler></xp:button>
答案 0 :(得分:4)
此示例将String的内容下载为txt-File。
希望有所帮助。
var textBuffer = "some text here";
var con = facesContext.getExternalContext();
var response:com.ibm.xsp.webapp.XspHttpServletResponse = con.getResponse();
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
response.setHeader("Content-Disposition", "attachment; filename=\"file.txt\"" );
response.getOutputStream().write(textBuffer.getBytes());
facesContext.responseComplete();
答案 1 :(得分:0)
您应该在SSJS中初始化Java代码。
importPackage(Java class) // whole Java class name with package !!!not string
var tmp = new ImportedClassName()
tmp.run() // run all logic
希望我能帮到你。最好的问候。