Skip to main content
Information Mgmt
New to Lotus
How to buy
Live demos
Technical library
Forums & community
Java™ technology
Open source
SOA and Web services
Web development
My developerWorks
About dW
Submit content

developerWorks  >  Lotus  >  Forums & community  >  Notes/Domino 4 and 5 Forum

Notes/Domino 4 and 5 Forum



SOLUTION: Checking Attachment Size Without Saving
Kevin Pettitt 29.Mar.04 05:15 PM a Web browser
Domino Designer -- Forms 5.0.10 Windows 2000

Several threads cover the topic of preventing the attachment of overly large files to a Notes document, and one major hurdle seems to be how to do so without first saving the document, let alone without first attaching the file. Without covering all the reasons why this is a challenge, I thought it might be helpful for me to detail the solution that I came up with after reading several posts in this forum and piecing everything together.

Design Goals:

1 - Limit the allowable size of attachments (total size in this case, not file by file) to any specific document
2 - Since it can take an eternity to attach a file that may ultimately be disallowed (the longer the eternity, the greater the liklihood it will be refused), we ideally want to check the size of a file BEFORE it is attached to a document. This translates into the need to check the filesize of a file at the file system level.
3 - Preserve whatever user-friendliness that already exists in the attachment process (I know "What user-friendliness?"). In short, when we attach files, the open file dialog should continue to open to the directory we last accessed, and not always back to the Notes Data directory.

1 - Create a subform called "Attachments"
2 - Put the following code in the Global Declarations:

Dim ws As NotesUIWorkspace
Dim Session As NotesSession
Dim db As NotesDatabase
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim GlobalSaveFlag As Integer 'will be used to check whether document was actually saved later on

Declare Function NEMGetFile Lib "NNOTESWS" Alias "NEMGetFile" _
( zero As Integer, Byval filename As String, Byval filter As String, Byval title As String ) As Integer

3 - Put this function in the subform globals:

Function LocalBrowse(title As String, default As String, filter As String) As String
Dim filename As String*1024
filename = default
If filter = "" Then filter = "All Files|*.*|"
status% = NEMGetFile(0, filename, filter, title)

Select Case status%
Case 0 : LocalBrowse = "" ' cancelled
Case 1 : LocalBrowse = Trim$(filename)
Case Else : Error 1000, "Error &H" & Hex$(status%) & " in LocalBrowse"
End Select
End Function

4 - PostOpen Event:

'Need to initialize several global variables
Set ws = New NotesUIWorkspace
Set Session = New NotesSession
Set db = Session.currentdatabase
Set uidoc = Source
Set doc = uidoc.document

5 - PostRecalc (@Functions instead of script)

@If(AttachFileName = ""; @Return(""); "");
@Command([EditGotoField]; "Attachments");
@Command( [EditInsertFileAttachment] ; AttachFileName ; "0" );
FIELD AttachFileName := "";

6 - QuerySave:

Dim DBProfile As NotesDocument 'optional - if you want to be able to configure your size limit
Dim SizeLimit as Long

GlobalSaveFlag = Continue ' So that if anything else modifies Continue, it is detected.

Set DBProfile = db.GetProfileDocument("DBSettings") 'This is my generic DB Profile, but you can use whatever you want here

'Either use this Profile Document somehow to store the limit, or hard code the limit (in bytes) here
SizeLimit = DBProfile.AttachSizeLimit(0)
'SizeLimit = 1048576 '1 MB

'Let's add up the total size of existing attachments to see if it exceeds our limit
'This script should only be necessary if a user was sneeky and used a traditional "Attach"
'command from the file menu instead of the buttons for selecting a file
If doc.AttachmentSizeTotal(0) > SizeLimit Then
Msgbox "You have exceeded the 1 MB limit on attachments. " & Chr(10) & Chr(10) & _
"Please remove attachments or compress your file(s)." , 16, "Attachment Error"
Continue = False
Exit Sub
Continue = True
End If

7 - QueryClose:

'Reset the AttachFileName field if we've left it filled and have saved the document
'Probably redundant but just in case
If GlobalSaveFlag = True And doc.AttachFileName(0) <> "" Then
doc.AttachFileName = ""
Call doc.Save(True, False)
End If

8 - Create a button labeled "Attach File" with the following lotuscript:

Sub Click(Source As Button)
Dim NewFilename As String
Dim currentpath As String
Dim DBProfile As NotesDocument
Dim verLen As Long
Dim SizeLimit as Long

Set DBProfile = db.GetProfileDocument("DBSettings") 'This is my generic DB Profile, but you can use whatever you want here

'Either create this Profile Document somehow to store the limit, or hard code the limit (in bytes) here
SizeLimit = DBProfile.AttachSizeLimit(0)
'SizeLimit = 1048576 '1 MB

NewFilename = LocalBrowse("Attach File", "", "")
doc.AttachFileName = NewFileName
currentpath = Strleftback(NewFilename, "\")

'This will reset the default browse directory to the one just searched
Call Session.SetEnvironmentVar( "FileDlgDirectory" , currentpath)

verLen& = Filelen(doc.AttachFileName(0)) 'the FileLen function is the real trick to all this

'Let's add up the total size of existing attachments AND the new file selected to see if it exceeds our limit
If (verLen& + doc.AttachmentSizeTotal(0)) > SizeLimit Then
Msgbox "This file will exceed the 1 MB limit on attachments. " & Chr(10) & Chr(10) & _
"Please remove attachments or compress your file(s)." , 16, "Attachment Error"
doc.AttachFileName = ""
doc.AttachmentSizeTotal = doc.AttachmentSizeTotal(0) + verLen&
End If

Call uidoc.Refresh 'triggers the postrecalc event which will complete the attachment process

End Sub

9 - Create an editable text field called "AttachFileName". Don't enter any formulas, and hide the field from view.

10 - Create a hidden, computed number field called "AttachmentSizeTotal" with the following formula:

REM "Do not truly recalculate this field if we\'re in the middle of an attachment process";
tmp := @If(AttachFileName = ""; @Sum(@AttachmentLengths); AttachmentSizeTotal);
@If(@IsError(tmp); 0; tmp)

11 - Now create the actual Rich Text field that will store the attachments. Call this editable field "Attachments", and deselect the "show field delimiters" checkbox (this will allow you to make it "look" like a computed field to users and help prevent them from doing the normal "File - Attach..." routine that would subvert our goals.

That's all you need to do. Simple huh? :-). Assuming you've done it all correctly (and that I've described it correctly), the user should be able to use our button to select a file for attachment, and continue to click it until they've selected a file that will put them over the limit. At that point, the attachment operation will stop and they will see a warning message. So, the next time, your users try to attach a 100MB file, they won't have to wait until after lunch to find out they did a bad thing. If they somehow manage to attach files that exceed the limit using the normal "File - Attach..." approach, the querysave will catch that situation. So to recap, the three main "breakthroughs" in this approach are as follows:

1 - Use of the NEMGetFile API function to call a normal Open File dialog
2 - Writing a value for the most recent directory to the FileDlgDirectory environment variable in the Notes.ini
3 - Using the FileLen function to check the size of a file in the user's file system, given the filepath as input

If you have any questions or problems getting this to work, feel free to contact me.

Kevin Pettitt

SOLUTION: Checking Attachment Size ... (Kevin Pettitt 29.Mar.04)
. . RE: SOLUTION: Checking Attachment S... (Kenneth Haggman... 29.Mar.04)
. . . . great post (Patrick Kwinten... 27.Apr.10)
. . RE: SOLUTION: Checking Attachment S... (Sherwin Delfin 11.Nov.11)

  Document options
Print this pagePrint this page

 Search this forum

  Forum views and search
Date (threaded)
Date (flat)
With excerpt
Advanced search

 RSS feedsRSS
All forum posts RSS
All main topics RSS
More Lotus RSS feeds

Forum use and etiquette
Native Notes Access
Web site Feedback

  Lotus Support
Lotus Support
Product support pages index
Search knowledge base (Technotes)
Search support downloads
Lotus Support RSS

IBM Composite Applications
IBM Mashup Center
IBM Connections
IBM Docs
IBM Forms
IBM Mobile Connect
IBM Sametime
IBM SmartCloud for Social Business
IBM Web Experience Factory
Lotus Domino
Lotus Domino Designer
Lotus Expeditor
Lotus Foundations
Lotus iNotes
Lotus Instructor Community Courseware
Lotus Notes
Lotus Notes & Domino Application Development
Lotus Notes Traveler
Lotus Protector
Lotus Quickr
Lotus Symphony
IBM Web Content Manager
WebSphere Portal

 Lotus Forums
Notes/Domino 9.0
Notes/Domino 8.5 + Traveler
Notes/Domino XPages development forum
Notes/Domino 8
Notes/Domino 6 and 7
Notes/Domino 4 and 5
IBM Connections
IBM Forms
IBM Mobile Connect
IBM Sametime
IBM SmartCloud Notes
IBM SmartCloud Meetings
IBM Web Content Manager
Lotus Domino Document Manager
Lotus e-learning
Lotus Enterprise Integration
Lotus Expeditor
Lotus Protector
Lotus Quickr
Lotus SmartSuite
Lotus Symphony
Lotus Symphony Developer Toolkit Support
Lotus Workflow