This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Mar 3, 2012, 12:02 AM
35 Posts
topic has been resolvedResolved

Write binary files from rich text field on file system

  • Category: Server Side JavaScript
  • Platform: Windows
  • Release: 8.5.3
  • Role: End user,Developer,Administrator
  • Tags:
  • Replies: 2
 
 Good evening Forum,
 
I'm looking for a way to extract several files contained in a binded rich text field onto the file system (on a network share)
 
I don't want user interaction, just click a button to extract / create the files, no popup to allow download....
 
I think I can do something with the next piece of code: 
 
var inPath:string = requestScope.filepath;
var n:int = inPath.lastIndexOf(".");
var outPath:string = inPath.left(n) + "Copy" + inPath.right(inPath.length - n);
var inStream:NotesStream = session.createStream();
if (inStream.open(inPath, "binary")) {
if (inStream.getBytes() > 0) {
var outStream:NotesStream = session.createStream();
if (outStream.open(outPath, "binary")) {
if (!outStream.isReadOnly()) {
do {
var buffer = inStream.read(32767);
outStream.write(buffer);
} while (!inStream.isEOS());
} else requestScope.status = "Output file exists and is read-only";
outStream.close();
} else requestScope.status = "Output file open failed";
} else requestScope.status = "Input file has no content";
inStream.close();
} else requestScope.status = "Input file open failed";
 
 
I already use something similar with HTML text contained in an edit box that I create a file from. Also using outStream.writeText.
 
So I don't think it's possible to extract the files without having to user accept the download, but It should be possible to convert to files to binary and write them like I already do for the HTML content. 
 
I hope someone will be able to help me with this? 
 
Thanks in advance, 
 
Chris 
 
======================================= 
 
Hi Forum, 
 
In the meantime I continued testing and next code bring me close to what I need, except that the created file doesn't have the right content... Probably problem with the buffer.
 
var strId:String = "2F509C8CD8D5F9A6C125789D0066DD06";
var strFile:String = "image.gif";

//retrieve target document from current database
doc = database.getDocumentByUNID(strId);

var eo:NotesEmbeddedObject = doc.getAttachment( strFile );

strFile = eo.getName();

var outPath:string = "\\\\Server\\XXX\\XXX\\"; //Path for output
var cdir = new java.io.File(outPath);
if (cdir.exists()) {
var outFileName:String = strFile;

var fos = new java.io.FileOutputStream(outPath + outFileName);
var osw = new java.io.OutputStreamWriter(fos);
var outputStream:NotesStream = session.createStream();
var buffer = new byte[4 * 1024]; // 4K buffer
var bytesRead;
var bufferedIS = new java.io.BufferedInputStream( eo.getInputStream());
while ((bytesRead = bufferedIS.read(buffer)) != -1) {
//outputStream.write(buffer, 0, bytesRead); //Error
outputStream.write(buffer);
}
outputStream.getContents(osw);
osw.close();
bufferedIS.close();
eo.recycle();
outputStream.close();
 
 
Thx in advance for the assistance. 
 
Chris 
Mar 3, 2012, 5:39 PM
272 Posts
Re: Write binary files from rich text field on file system
Hi,
 
maybe I am not understanding the problem but why are you not use the extractFile method of embeddedObject?
 
Sven
 
Mar 3, 2012, 6:44 PM
35 Posts
Re: Write binary files from rich text field on file system
 Sven,
 
Thx you're right, it's working like a charm... no idea why I though I had to convert to a binary file! 
 
You helped me a lot. 
 
Have a nice weekend. 
 
chris 

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal