Solved

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

Posted on 2003-11-21
2
7,971 Views
Last Modified: 2012-05-04
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
Comment
Question by:qfren
2 Comments
 
LVL 23

Assisted Solution

by:adilkhan
adilkhan earned 100 total points
ID: 9797880
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
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 400 total points
ID: 9808936
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now