If there is one subject that gets its fair amount of airtime on the Notes forums it's the matter of dealing with file attachments. Whether it's moving them to or from Rich Text fields, stopping them appearing at the bottom of every page, controlling the types of files that can be uploaded or limiting the size of uploaded files there is endless discussion.
In this article I am going to offer an approach to the latter of these issues - limiting the size of files.
The Problem:
There are lots of applications where you require the user to upload files. Even if you trust your users to only upload the files they should this doesn't mean they won't accidentally upload a huge file that puts extra burden on the server. Not to mention taken up extra space on the disk. What we need is a way to put a reasonably cap on the size of files that can be uploaded.
One method is to use the server document to limit the size of files that can be sent to the server by a form post. As below (note that 0 means NO limit):
There are problems with this though. This limit will apply to all databases on the server. More than likely the limits you want to enforce will apply to different forms and be for different sizes.
And there's an even bigger problem. Imagine you set the limit to 1024KB (1MB) and somebody tries to upload an file that is 1.5MB. This is what they will see:
Under no circumstance is it acceptable to have a user have to see this. What we need is a way to politely and elegantly inform the user that the file they uploaded is too big. We need some code.
The Solution:
Before I start I want to say that, as far as I am aware, there is no real way to test the size of a file from the browser. For the sake of this article I am assuming we are limited to the simple type="file" input element and no fancy plugins or whatnot. To check the size of the file we are going to have to upload the file to the server and then do our calculations there.
Because of the way things happen in Domino and the way it processes submitted forms we are going to have to do out coding in the Web Query Save event. The reason for this is that it's the last thing that happens and we can be sure that all the files are in place and ready for inspection.
In coming up with a solution I tried two approaches. First with some simple @Functions and then with LotusScript code. The reason that I moved on to LotusScript was that the @Function method is not really something I would recommend. I'll explain why at the end of the @Function section. If you want to skip this to the better method then go straight to the LotusScript section now. Otherwise, if you want to skip all of my rabble and just download the attached example database, it's here.
Using @Functions:
It's pretty obvious that we can do it in LotusScript. You can almost everything in LotusScript. You don't always have to though and I always like to have a go with @Functions before I start Dimming things. All we need in this case is to check the size of the biggest attachment which we can get at using @AttachmentLengths. If the biggest file is bigger than our limit we set the SaveOptions field to "0" (means the document is not saved) and set the value of $$Return (where the browser gets redirected to) to a URL that ends with a keyword that will cause a warning message to display. Here's the code I came up with.
@If(
@AttachmentLengths = "";
@SetField("$$Return"; "[/" + @WebDbName + "/0/" + @Text(@DocumentUniqueID) +"?OpenDocument]");
@If(
@Max(@AttachmentLengths) > 1000000;
@If( @IsNewDoc;
@Do(
@SetField("SaveOptions";"0");
@SetField("$$Return"; "[/" + @WebDbName + "/formula?OpenForm&FileTooLarge=True]")
);
@Do(
@SetField("SaveOptions";"0");
@SetField("$$Return"; "[/" + @WebDbName + "/0/" + @Text(@DocumentUniqueID) +"?EditDocument&FileTooLarge=True]")
)
);
@SetField("$$Return"; "[/" + @WebDbName + "/0/" + @Text(@DocumentUniqueID) +"?OpenDocument]")
)
)
redirect = "[/" & dbWebPath & "/0/" & doc.UniversalID + "?OpenDocument]"
If doc.HasEmbedded = True Then
files = Evaluate(|@AttachmentNames|, doc)
Forall v In files
If v <> "" Then
Set obj = doc.GetAttachment( v )
If obj.FileSize > doc.DocFileSizeLimit(0) Then
Call obj.remove
If doc.IsNewNote Then
redirect = "[/" & dbWebPath & "/script?
OpenForm&FileTooLarge=True]"
Else
redirect = "[/" & dbWebPath & "/0/" &
doc.UniversalID +
"?EditDocument&FileTooLarge=True]"
End If
End If
End If
End Forall
End If
Call doc.ReplaceItemValue( "$$Return", redirect )
Copyright © 2000 - 2025 Jake Howlett of Rockall Design ltd. This article was printed from codestore.net