Search code examples
xpagesxpages-ssjs

How do I get the filename programmatically to use with this code?


The code below is the computed image source for an image control on my custom control. If I run it just like it stands it puts in photo.jpg and works fine. The problem is I want to pass that value programmatically. I have an upload control (fileUpload1) and a download control (fileDownload1) on the custom control as well. I've tried document1.photo which is where the jpg file is stored. I've tried several variations of currentdocument.getitemvalue, getitem, getattachments. But, so far, I have found no way to do this. In addition, I have tried setting the value of filename to the value of the upload control and the download control with no success.

I know this is probably simple, but, as a client developer, it's not simple to me.

Any help would be greatly appreciated.

var doc:NotesDocument = currentDocument.getDocument ();
var UID:string = doc.getUniversalID().toString();
var fp:string = database.getFilePath();
var filename = "photo.jpg"

return '/0/' + UID + '/$file/' + filename

Thanks, MJ


Solution

  • There may be other ways to achieve this but here's what I usually do:

    with image controls I need to be aware that computed resource URLs are in most cases prepended with the database's relative path ending with the database filename. So all I have to add is the usual file attachment URL as in

    /0/docUnid/$File/imageFileName
    

    If there is a chance that you have more than one file attached to your doc I need to find out which one of them is the one I wish to display. This can be done by looping through

    currentDocument.getAttachmentList(fieldName)
    

    which delivers a java.util.List of NotesEmbeddedObject elements. Using methods like .getName() I then can find the one file you're looking for.

    EDIT:
    quick example using a repeat to display multiple images sattached to a single richtext field (there may be better solutions esp. regarding the way to filter unwanted file types; I'm using file extensions, only allowing .jpg, .png and .gif). Images are displayed as simple html list elements:

    <ul>
        <xp:repeat id="repeat1" rows="30" var="pic">
            <xp:this.value><![CDATA[#{javascript:
        var att:java.util.List=docCar.getAttachmentList("Body");
        var it=att.listIterator();
        //new empty array, to be used as the repeater's datasource
        var arr=[]; 
        //build base path for each image file
        var unid=docCar.getDocument().getUniversalID();
        var p="/0/" + unid + "/$File/";
        while(it.hasNext()){
            var e:NotesEmbeddedObject=it.next();
            if(e.getName().endsWithIgnoreCase(".png") || 
                e.getName().endsWithIgnoreCase(".jpg") || 
                e.getName().endsWithIgnoreCase(".gif")){
                //push complete filepath + image filename to array
                arr.push(p + e.getName());
            }
        }
        return arr;}]]></xp:this.value>
            <li>
                <xp:image url="#{javascript:pic}" id="image1">
                </xp:image>
            </li>
        </xp:repeat>
    </ul>
    

    Hope this helps