Hi Ben, all

This is also close to what I had come up with. Actually, I had to avoid the use of ImageIO because we're deploying on Mac OSX and their ImageIO is buggy im sure...

Anyway, this server-side solution has the following problems:
1) If you try to upload a 1GB file, you arn't stopping anybody
2) You're reading all that data into memory, again if you upload the huge file then you're going to lead to issues

In my case, we're hoping that some non-I.T. people will be using the app and I can garantee they'll try to upload a billboard-sized TIFF. Hence the need to validate client-side.

It would be nice if I could set the value field on a file type (which is what is allowed in the 4.01 spec) but it is not supported in most browsers.

Do I have to actually implement my own uploading code? Ouch :(

Ben Dotte wrote:
I wrote a validator for this but I'm not totally sure this doesn't end
up uploading the whole file anyway.. doesn't seem like it though.

public void validate(IFormComponent field, ValidationMessages messages,
Object object) throws ValidatorException {
        IUploadFile imageFile = (IUploadFile)object;
        InputStream fis = imageFile.getStream();
        ImageInputStream iis = null;

try {
                iis = ImageIO.createImageInputStream(fis);
                Iterator imageReaders = ImageIO.getImageReaders(iis);
                if (!imageReaders.hasNext())
                {
                        // There are no image readers for this file
                        fis.close();
                        throw new ValidatorException("Invalid image
file.");
                }
                ImageReader firstReader =
(ImageReader)imageReaders.next();
                String imageFormat =
firstReader.getFormatName().toLowerCase();
                if (!imageFormat.equals("jpeg") &&
!imageFormat.equals("jpg") &&
                        !imageFormat.equals("gif") &&
!imageFormat.equals("png"))
                {
                        // This is a valid image but we only accept
jpeg, jpg, gif, or png
                        fis.close();
                        throw new ValidatorException("The image format
must be JPG, GIF, or PNG.");
                }
                BufferedImage image = ImageIO.read(iis);
                if (image.getWidth() > maxWidth || image.getHeight() >
maxHeight)
                {
                        // This image is too big
                        fis.close();
                        throw new ValidatorException("The image exceeds
the maximum width/height.");
                }
                fis.close();
} catch (IOException ioe) {
                // There was some other problem uploading the image
                throw new ValidatorException("Invalid image file.");
        }
finally {
                if (iis != null) {
                        try { iis.close(); } catch (IOException ioe) {}
                }
                if (fis != null) {
                        try { fis.close(); } catch (IOException ioe) {}
                }
        }
}

HTH

Ben

-----Original Message-----
From: Daniel M Garland [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 13, 2005 10:01 AM
To: [email protected]
Subject: Client-side validated uploads

Hi all,

I'm trying to figure out how to get around a brick wall:

I want to validate an image file based upon its dimensions and filesize before allowing the file to be written up to the server. I thought about

achieving this using a signed applet to read the file, and then pass an acceptable filename to an Upload component. However, the value field will not be supported in most browsers, so is there any other way I can use the Uploading features of Tapestry by somehow extending Upload?

If not, where is the code that does this? I hear something about the commons project at apache but currently jakarta.apache.org/commons/ appears to be down.

Thanks for your help in advance.

--
Dan Garland
------------------------
[EMAIL PROTECTED]
icq: 120963437
aim: dmgarland1767


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email ______________________________________________________________________

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to