Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8630059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:58:03+00:00 2026-06-12T08:58:03+00:00

I have been given this JavaScript script to restrict max file size before it

  • 0

I have been given this JavaScript script to restrict max file size before it is uploaded, but I’m not sure how I would implement it with my current code.

Here is the JavaScript:

<script type="text/javascript">

            function handleFileSelect(evt) {
  var files = evt.target.files; // FileList object

  // files is a FileList of File objects. List some properties.
  var output = [];
  for (var i = 0, f; f = files[i]; i++) {
   console.log(f.size);        /*<--Here is your size of the file! :D*/
  }
}

 document.getElementById('files').addEventListener('change', handleFileSelect, false);

</script>

And my HTML:

<label>Upload Image</label>
<input type="file" name="image" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="5120" /></p>
<p><input type="submit" id="submit" value="Upload" />

MODIFIED CODE:

  <p><label>Upload Image</label>

  <script type="text/javascript">
 function handleFileSelect(evt) {
   var files = evt.target.files; // FileList object
    var max_size = 5120; // Max file size

  // files is a FileList of File objects. List some properties.
  var output = [];
   for (var i = 0, f; f = files[i]; i++) {
    // console.log(f.size);
     if(f.size > max_size) { // Check if file size is larger than max_size
     alert("Sorry, but the file that you selected is too large. Please upload a file that is no larger than " + max_size + " KB.");
      return false;
    } // end if
   } // end for loop
 } //  end function

 document.getElementById('files').addEventListener('change', handleFileSelect, false);
 </script>

                <input type="file" name="image" id="files"/><br />
 <input type="hidden" name="MAX_FILE_SIZE" value="5120" /></p>
                <p><input type="submit" id="submit" value="Upload" />
            </p>
       <p>&nbsp;</p>
 </form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:58:04+00:00Added an answer on June 12, 2026 at 8:58 am

    Just add an id attribute to the file input, since the script is looking for an element with an id of files:

    <label>Upload Image</label>
    <input type="file" name="image" id="files" /><br />
    <input type="hidden" name="MAX_FILE_SIZE" value="5120" />
    <p><input type="submit" id="submit" value="Upload" />​</p>​​​​​​​​​​​​​​​​​​​​​​​​​​​
    

    http://jsfiddle.net/RnuT8/

    By the way, be aware that this JavaScript can be easily bypassed by simply disabling JavaScript or directly uploading the file to the server without using your form. I recommend that you check the file size with your server side PHP upload script as well.

    Modified Code

    I have modified your script so you can enter a max size value and the script will check if it is greater than max_size or not:

    window.onload = function() {
    
        function handleFileSelect(evt) {
          var files = evt.target.files; // FileList object
          var max_size = 5120; // Max file size
        
          // files is a FileList of File objects. List some properties.
          var output = [];
          for (var i = 0, f; f = files[i]; i++) {
           // console.log(f.size);
            if(f.size > max_size) { // Check if file size is larger than max_size
              alert("Sorry, but the file that you selected is too large. Please upload a file that is no larger than " + max_size + " KB.");
              return false;
            } // end if
          } // end for loop
        } //  end function
    
       document.getElementById('files').addEventListener('change', handleFileSelect, false);
    }
    

    But remember, this is passable and the file size should be validated on the server as well (the JavaScript is good too and speeds up stuff, but if you want security you must validate it on the server as well).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that I have this resultset structure (superfluous fields have been stripped) Id |
I have been struggling with this for a while now. given a set of
I know that this question might have been asked like 100 times, but, believe
I have been given a task to create a Python file that will take
Ok guys I know this question has been asked before but I am very
This javascript I have is supposed to be very simple. It is, but IE
I have been given this code by the ui designer and I dont know
Before I get flamed with references to manuals, I have been researching this for
Okay. Now I give up. I have been playing with this for hours. I
I have been given control of a web site in Classic ASP. What is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.