XPages to create .txt File via SSJS

2019-09-03 04:12发布

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

Screenshot of confirmation

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>

2条回答
祖国的老花朵
2楼-- · 2019-09-03 04:24

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-09-03 04:36

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();
查看更多
登录 后发表回答