Hi Miguel,

AFAIK (and I even wrote a mail to Mike Shrag, who wrote this code) there is no 
real good way to catch an ERXAttachmentExceedsLengthException.

What I did was:

1. in your application class, override the "handleException" method and check 
if anException isinstanceof ERXAttachmentExceedsLengthException or any other 
kind of exception.

If the anException is a ERXAttachmentExceedsLengthException, try to go back to 
the page where you were coming from. In my example I stored the last page with 
the ERAttachmentUpload in my session, so I can restore it (and set an error 
message for the user):

public WOResponse handleException(Exception anException, WOContext context) {

        if (anException instanceof ERXAttachmentExceedsLengthException) {
                ERXComponent nextPage = ((Session) 
context.session()).lastPageBeforeAttachmentExceedsLengthException();

                if (nextPage instanceof Main) {
                        ((Main) 
nextPage).setErrorMessageFromException("<b>ERROR: This attachment is too 
big.</b> ...");
                }
                return nextPage.generateResponse();
        } else {
                OtherExceptionResponsePage nextPage = 
(OtherExceptionResponsePage)
                        
pageWithName(de.i4innovation.app.components.OtherExceptionResponsePage.class);
                nextPage.setTheException(anException);
                return nextPage.generateResponse();
        }
}


2. There is a Javascript/jQuery solution to check the file-name and file-size 
BEFORE you actually upload the file to your server.

It assumes your wo:ERAttachmentUpload has the id 'uploadButton'. The save 
button has the id 'saveButton' and an initially not displayed div has the id 
'uploadMessage'.

With jn("#uploadButton").change(function(evt) ... you bind a function to the 
Upload button that is fired during the onChange event -> that means before the 
file is actually uploaded.


It works like this:

<p><wo:ERAttachmentUpload id="uploadButton" configurationName = 
"Document.attachment" attachment = "$theDocument.erAttachment" editingContext = 
"$session.defaultEditingContext" />
        <wo:submit id="saveButton" style="display: none;" value = "save" action 
= "$saveAttachmentAction" />
        <div id="uploadMessage" style="border: 1px solid red; padding: 5px; 
color: red; display: none;"></div>

        <wo:if condition = "$errorMessageFromException">
                <div style="border: 1px solid red; padding: 5px; color: 
red;"><wo:string value = "$errorMessageFromException" escapeHTML = "$false" 
/></div>
        </wo:if>
</p>
<script>
        /* extend js' string with an endsWith function */
        function endsWith(str, suffix) {
                return str.indexOf(suffix, str.length - suffix.length) !== -1;
        }

        /* define the max file size */
        var maxFileSize = 20971520;

        /* run jQuery in noConflict mode */
        var jn = jQuery.noConflict();

        /* attach this function to the file-upload-button's onChange event */
        jn("#uploadButton").change(function(evt) {
                if (window.File && window.FileReader && window.FileList && 
window.Blob) {
                        // Great success! All the File APIs are supported.
                        var files = evt.target.files; // FileList object
                        // files is a FileList of File objects. List some 
properties.
                        for (var i = 0, f; f = files[i]; i++) {
                                var msgDiv = 
document.getElementById('uploadMessage');
                                var msgButton = 
document.getElementById('saveButton');
                                if (f.size > maxFileSize) {
                                        var fSize = f.size/1000000;
                                        var msg = "<b>ERROR: This attachment is 
too big.</b><br />Maximum file size is: <b>10MByte</b>, actual file size: 
<b>"+fSize.toFixed(2)+" MByte</b>.<br>Please upload a smaller file!";
                                        msgDiv.style.display = "block";
                                        msgDiv.innerHTML = msg;
                                        msgButton.style.display="none";
                                } else {
                                        if (!endsWith(f.name.toLowerCase(), 
"zip")) {
                                                var msg = "<b>ERROR: This file 
is not a zip file.</b><br />Please choose a zip file.";
                                                msgDiv.style.display = "block";
                                                msgDiv.innerHTML = msg;
                                                msgButton.style.display="none";
                                        } else {
                                                msgDiv.style.display = "none";
                                                msgButton.className= "btn 
btn-success";
                                                msgButton.style.display = 
"block";
                                        }
                                }
                        }  // end for-loop
                } else {
                        alert('CAVE: This browser doesn\'t support attachment 
size validation!');
                        
document.getElementById('saveButton').style.display="block";
                }
        });
</script>
</wo:form>

If you like I would send you the demo project I've developed. It's only 27kb, 
but I'm not sure to post it on the list here.


C.U.CW
--
The three great virtues of a programmer are Laziness, Impatience and Hubris. 
(Randal Schwartz)

> On 31.07.2015, at 00:37, Miguel Angel Torres Avila <[email protected]> wrote:
> 
> Hi List,
> 
> I am doing my first Project using ERAttachments, it is great by the way!
> 
> But I am not sure how to catch an ERXAttachmentExceedsLengthException and I 
> have searched without luck.
> 
> Is there any documentation about it?
> 
> Thanks in advance.
> 
> 
> 
> _______________________________
> Ing. Miguel Angel Torres Avila
> Director General
> Tel: +52 (33) 3367 1892
> Cel: +521 (33) 3106 8758
> E-mail: [email protected]
> www.toracom.net
> 
> Antes de imprimir, piense en el Medio Ambiente. Before printing think about 
> the Environment. Avant d'imprimer, pensez à l'Environnement
> 
> 
> 
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list      ([email protected])
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/webobjects-dev/cw%40i4innovation.de
> 
> This email sent to [email protected]

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to