Forum Moderators: open

Message Too Old, No Replies

Problem with check file size with Javascript

         

Marfola

2:49 pm on Nov 21, 2007 (gmt 0)

10+ Year Member



I’m trying to find the file size using javascript, before it get uploaded into the server using the following Javascript (I don’t want to use ActiveX because of compatibility problem).

function imageSize (pathField, previewName)
{
var str = '';
var path = pathField.value;
document[previewName].src = 'file://'+path.replace(/\\/gi,'/');
var img = new Image();
img.src = 'file://'+path.replace(/\\/gi,'/');
var size_pic = document.getElementById(previewName).fileSize;
str = "File size="+size_pic;
return (str);
}

my HTML who call it:
<form name="myform" method="post" action="<myaction>">
<input name="upload_file" type="file" id="upload_file" size="28" value="" onChange="Javascript:imageSize(document.myform.upload_file,'tmpImg')"><br>
<img src="tmp.gif" name="tmpImg" width="1" height="1"/>
<input type="Submit" name="upload" value=”upload">
</form>

If I test my function offline it always manage correctly. Unfortunately, when I test it online (i.e. when I copy the html with the above javascript on my webserver Apache), it manages only if files I upload are little. Normally it returns -1!

Could someone help me to find the problem?

rocknbil

8:45 pm on Nov 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Browser security does not allow this. You **may** be able to do it in Internet Exploder because when you load the page locally it warns you that this page is running ActiveX Controls and you've gone ahead and disabled it.

If I could read file size on **your** computer via the file upload, I could read other files, couldn't I?

You can do this if your code is CA signed, and I've seen some Ajax attempts at solutions, but cannot verify if they work at all or with consistency. It's just not worth it. :-)

Best shot is to read the file size server-side on upload, or implement a Java applet.

Marfola

10:07 am on Nov 22, 2007 (gmt 0)

10+ Year Member



I know that also Java applets got the same problem as Javascript, i.e. they are executing client-side so they cannot access the local filesystem.
In your reply you suggest Ajax solutions. Could you please give me more details? Or, do you know other solutions?
Thanks.

Marfola

2:35 pm on Nov 27, 2007 (gmt 0)

10+ Year Member



I have a clarification, my Javascript sometimes run correctly. In fact if images are lesser than 100KB all it's OK, my script return the correct size. If images are greater the Javascript don't load them and return size -1.
Do you have some suggestions? Or do you know other way to solve the problem?