0
votes

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
You're not telling us, what you have tried so far. Try to be more specific: what are the results you're getting so far, and why aren't they meeting your expactations?Lothar Mueller
I tried this kind of samples www-10.lotus.com/ldd/ddwiki.nsf/dx/…Cumhur Ata
Again: what kind of result did you get from that sample code, error messages? Faulty text file? Which one of the samples from the wiki did you try, and what was it that you didn't like about its result?Lothar Mueller
I used the code Lars sent. but the only problem is when i try to run with this code It asks to save file. I need to save the file without confirmation in to my hard drive. I added what i tried to save it automaticallyCumhur Ata

2 Answers

4
votes

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

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.