Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7994
  • Last Modified:

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



0
qfren
Asked:
qfren
2 Solutions
 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now