Image upload dimension check

  • Thread starter Gianpiero Colagiacomo
  • Start date
G

Gianpiero Colagiacomo

Hi,

I'm trying to use the following code (found online - can't remember where)
to validate images being uploaded to my server:

function checkImageUpload (fileName) {
if (document.layers && location.protocol.toLowerCase() != 'file:' &&
navigator.javaEnabled()){
netscape.security.PrivilegeManager.enablePrivilege('UniversalFileRead');
}
var msg = '';
var img = new Image();
img.src = 'file:///' + fileName;
if (img.width != 180 || img.height != 90){
msg = "The selected image dimensions are not valid. The image must be 180
x 90 pixels.\n\n";
msg = msg + "Your selected image dimensions are " + img.width + " x " +
img.height;
alert('The required information is incomplete or contains
errors:\t\t\t\t\t\n\n'+msg);
return false
} else {
return true
}
}

Trouble is it won't work in Netscape. I get 0 x 0 pixels reported as being
the image size although the if(document.layers... line should be dealing
with Netscape issues (I think)...

What I ultimately want is to be able to make this script cross browsers
compatible and cross platform is possible...

Can anyone help me out? I'd prefer not to have to revert to validating the
file AFTER having uploaded it...

I also need to be able to make the script only accept jpg's and gif's - is
there an easy method of achieving this?

Cheers,

GP
 
G

Gianpiero Colagiacomo

Nobody - Thanks I'll try using a dummy image and see if that solves it...

As for the 'bizarre logic' - as I said before the script came from someone
else - I only altered it a little and that line was left alone as I had no
idea what it was attempting to do...

What would you suggest for checking if the security object IS present (cross
browser)?

Cheers,

GP
 
L

Lasse Reichstein Nielsen

Gianpiero Colagiacomo said:
I'm trying to use the following code (found online - can't remember where)

Never a good sign.
to validate images being uploaded to my server:

function checkImageUpload (fileName) {
if (document.layers && location.protocol.toLowerCase() != 'file:' &&
navigator.javaEnabled()){
netscape.security.PrivilegeManager.enablePrivilege('UniversalFileRead');
}

I'll assume this works for Netscape 4 with a non-file-protocol and
java enabled.

img.src = 'file:///' + fileName;
if (img.width != 180 || img.height != 90){

Here is a serious problem. Images are loaded asynchronelously. A local
file might be loaded fast enough that you this works ... most of the
time, but you should always wait for the image to load.

One way to do that, is to put the rest of the code in the image's onload
handler, and add an onerror handler for the case where it doesn't work.
Trouble is it won't work in Netscape.

Which Netscape?
There are Netscape 4 and Netscape 6/7, which are two completely unrelated
browsers.
What I ultimately want is to be able to make this script cross browsers
compatible and cross platform is possible...

Good luck. I don't think there is any way my browser will let you read
a local file.
I also need to be able to make the script only accept jpg's and gif's - is
there an easy method of achieving this?

No. You can probably check the filename's extension, but that proves nothing.

/L
 
J

Jim Ley

FYI layers are only present in NS4 and OmniWeb
(at current count.)

You might want to check that, Escape has 'em to...
| var msg = '';
| var img = new Image();
| img.src = 'file:///' + fileName;
| if (img.width != 180 || img.height != 90){
| msg = "The selected image dimensions are not valid. The image must be
180
| x 90 pixels.\n\n";

No idea why NS would come up with 0x0. Have you tried using a dummy image
instead of creating the object on the fly? In fact, that is almost
certainly it as the created image is hidden.

No, the problem is that the image hasn't loaded yet, you need to check
the size of the image after the image onload event has fired.

img=new Image()
img.onload=function() {
alert(img.height)
}
img.src="..."

Jim.
 
G

Gianpiero Colagiacomo

Ok guys - I get it - check the dimensions in server-side

Thanks...

BTW Jim Ley - your were right - I tried using settimeout on the dimension
check and it worked fine in NN7 and IE5.5. I didn't realise you could set
an onload handler which obviously is the better method.

GP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top