check the size with javascript before upload.....

hi:

look at this coding below..

this is a coding where user can upload the file to server...its works fine.


-------

<script>
var nfiles =3;
function Expand(){
  nfiles++
  var adh = '<BR> File '+nfiles+' :<input type="file" name="File'+nfiles+'">';
  files.insertAdjacentHTML('BeforeEnd',adh);
  return false;
}

</script>
<script>

function onSubmitForm() {
    var formDOMObj = document.frmSend;
      var confirmUpload
    if (formDOMObj.File1.value == "" && formDOMObj.File2.value == "" && formDOMObj.File3.value ==

"" )
        alert("No valid file(s) selected! Please press the browse button and pick a file.")
            
    else
        return true;
    return false;
}
</script>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0"

bordercolor="#9933CC">
  <tr>
    <td>
       <form name="frmSend" method="POST" enctype="multipart/form-data"

action="uploadTester.asp" onSubmit="return onSubmitForm();">
   <Div ID=files>
    File 1: <input name=File1 type=file onchange="CheckSize()"><br>
    File 2: <input name=File2 type=file onchange="CheckSize()"><br>
    File 3: <input name=File3 type=file onchange="CheckSize()">
   </Div>
   <Input Type=Button Value="Add file" OnClick=return(Expand())

Style="border=0;background=#6699FF;cursor:hand" id=Button1 name=Button1><font size="1"

face="Arial, Helvetica, sans-serif"><a href="javascript:alert('Click to add more file to upload

.');"> << What
  is this?</a></font> <br>
      <br>
        <input name=submit1 type=submit id=submit1 style="margin-top:4;" value="Upload">
      
    </form>
      
      </td>
  </tr>
</table>
<p><br>
 


------


However, now i wanna add some coding for << onchange="CheckSize()" >>


i wan to do the coding in javascript to that checksize() function so that,

fucntion will check so that when the user click the "browser" and choose a file,it will check the size of file first....If the size exceed 100K, there is a message --alert("File Size Exceed
100KB"). Then, that input will BLANK again so that user can re-choose..


One more thing hv to mention is as u can see,user can "Add file" to upload more than 3 file
right?So, maybe there is a user add until 10 files to upload. Let say, the first 5 files is
ok(means doenst exceed the size). But file number-6 was exceed the size, so ONLY the input
number-6 will blank; the previous 5 input(which doenst exceed the size) nothing change on it. Means every input will be check...



Let me know if u guys dont understand my Q



q



LVL 8
qfrenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib KhanSenior DeveloperCommented:
qfren This is not javaScript Issue.
javaScript is a Client Side Language, to check the File file you need to play with your FileUpload Component.
You can also Check the file Size VIa ActiveX or VbScript but that Requires Clients to have the ActiveX component as well.
0
David H.H.LeeCommented:
qfren,
If you dont want to check filesize from server size, you can use ActiveXObject to solve your problems as mentioned by adilkhan. Otherwise, you need to use Server-side language to solve the problems.
Here's my solutions about what you need. BUt, you need to choose 'Yes' when you see a pop-up message from activeXObject.

fileSize.html
==============
<script>
var nfiles =3;

function Expand(){
 nfiles++
 var adh = '<BR> File '+nfiles+' :<input type="file" name="File'+nfiles+'">';
 files.insertAdjacentHTML('BeforeEnd',adh);
 return false;
}

function onSubmitForm() {
   var formDOMObj = document.frmSend;
    var confirmUpload
   if (formDOMObj.File1.value == "" && formDOMObj.File2.value == "" && formDOMObj.File3.value ==

"" )
       alert("No valid file(s) selected! Please press the browse button and pick a file.")

   else
       return true;
   return false;
}

function CheckSize(objVal)
{
try
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var d=fso.GetFile(objVal);
if (eval(d.size)>102400)
alert('Exceed 100KB file size! Not allow to upload");
}
catch(err){
}
}
</script>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0"

bordercolor="#9933CC">
 <tr>
   <td>
     <form name="frmSend" method="POST" enctype="multipart/form-data"

action="uploadTester.asp" onSubmit="return onSubmitForm();">
  <Div ID=files>
   File 1: <input name=File1 type=file onChange="CheckSize(this.value)"><br>
   File 2: <input name=File2 type=file onchange="CheckSize(this.value)"><br>
   File 3: <input name=File3 type=file onchange="CheckSize(this.value)">
  </Div>
  <Input Type=Button Value="Add file" OnClick=return(Expand())

Style="border=0;background=#6699FF;cursor:hand" id=Button1 name=Button1><font size="1"

face="Arial, Helvetica, sans-serif"><a href="javascript:alert('Click to add more file to upload

.');"> << What
  is this?</a></font> <br>
     <br>
       <input name=submit1 type=submit id=submit1 style="margin-top:4;" value="Upload">

    </form>

     </td>
 </tr>
</table>
<p><br>

Regards
x_com
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.