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("ERROR: This attachment is too
big. ...");
}
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:
/* 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/100;
var msg = "ERROR: This attachment is
too big.
Maximum file size is: 10MByte, actual file size:
"+fSize.toFixed(2)+" MByte.
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 = "ERROR: This file
is not a zip file.
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";
}
});
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.
(Randa