Skip to main content
    Country/region select      Terms of use
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks  >  Lotus  >  Forums & community  >  Best Practice Makes Perfect

Best Practice Makes Perfect

A collaboration with Domino developers about how to do it and how to get it right in Domino

Prompted by a question in the forums, I searched around and didn't find a good example of this technique, so here's a sample of confirming that a rich text field contains exactly one image, with restrictions on the dimensions.

Sub Querysave(Source As Notesuidocument, Continue As Variant)
        Dim doc As NotesDocument
        On Error Goto oops
        Source.Refresh True ' update rich text data into back end.
        Set doc = Source.Document
        Dim session As New NotesSession
        Dim stream As NotesStream
        Dim dxle As NotesDXLExporter
        Dim domp As NotesDOMParser
        Dim domd As NotesDOMDocumentNode
        Dim nl As NotesDOMNodeList
        Set stream = session.CreateStream
        Set dxle = session.CreateDXLExporter(doc, stream)
        dxle.RestrictToItemNames = "Body"
        ' at this point you might want to set additional properties for efficiency, e.g. to omit attachments
        ' please note that if you want to disallow certain types of content, you can use a Rich Text Lite field,
        ' so this is not the preferrred way to block users from adding an image.
        Set domp = session.CreateDOMParser(stream)
        Set domd = domp.Document
        Set nl = domd.GetElementsByTagName("picture")
        If nl.NumberOfEntries <> 1 Then
                Msgbox "You must have exactly one image in the Body field."
                Continue = False
                Exit Sub
        End If
        Dim picture As NotesDOMElementNode
        Set picture = nl.GetItem(1)
        Dim picX As Long, picY As Long
        picX = Clng("0"+Strleft(picture.GetAttribute("width"), "px"))
        picY = Clng("0"+Strleft(picture.GetAttribute("height"), "px"))
        If picX > 400 Or picY > 400 Then
                Msgbox "The image may not exceed 400x400 pixels."
                Continue = False
                Exit Sub
        End If
        If stream.Bytes > 160000 Then
                Msgbox "The image is too large. Please IMPORT the image from a GIF or JPEG file; do not paste graphics or import a BMP file. Thanks."
                Continue = False
                Exit Sub
        End If
        ' else: success!
        Exit Sub
        ' if a validation failure don't bother to display the error because user already saw one.
        If Err <> 4412 Then Msgbox "Error " & Err & " in Querysave line " & Erl & ": " & Error
        Continue = False
        Exit Sub
End Sub

Please note that the width and height properties refer to the dimensions before scaling; if the user drags the picture bigger or smaller these values will not change, but scaling values will be added to the DXL, so you might need to look at those in addition/instead.

I've done a crude size check by checking stream.Bytes; the user could exceed this limit by other things than having a too-large graphic. For instance, if the field contained a lot of text in addition to the image. To more precisely check the image size, you would have to check the length of the base64-encoded character node which is a second-level descendant of the element. E.g. you might see:


has a child (or or ) and the image data is a child of that node.

Andre Guirard | 9 September 2009 09:25:56 AM ET | Home, Plymouth, MN, USA | Comments (5)

Search this blog 


    About IBM Privacy Contact