XPages to create .txt File via SSJS

自古美人都是妖i 提交于 2019-12-12 05:13:22

问题


I need to create a text file with SSJS. I get data from a document then i will write all data in a text file. I tried to some solution which i find searching on the internet but I could not get result I needed.

Regards
Any Suggestion is appreciated
Cumhur Ata


UPDATE:

writer.endDocument();
facesContext.responseComplete();
writer.close();

UPDATE 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>

回答1:


This example will download the content of a String as a txt-File.

Hope that helps.

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



回答2:


You should initialize Java code in your SSJS.

importPackage(Java class) // whole Java class name with package !!!not string
var tmp = new ImportedClassName()
tmp.run() // run all logic

Hope I help. Best regards.



来源:https://stackoverflow.com/questions/35868865/xpages-to-create-txt-file-via-ssjs

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