Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Finding file size using javascript

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Thanks in advance.
Could any one please tell me how can I find the file size using javascript?
I want to know the file size prior to upload to server only.

Regards,
Sree
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript can not do that for security reasons.

Eric
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative is to create a signed applet or web start application which you embed in your JSP page.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check below issue of stack-overflow:

https://stackoverflow.com/questions/7497404/get-file-size-before-uploading
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this way :

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<!DOCTYPE html>
<html>
 
<head>
   <title>File Validation-1</title>
</head>
 
<body>
   <p>
       <input type="file"
              id="file"
              onchange="Filevalidation()" />
   </p>
 
   <p id="size"></p>
</body>
 
<script>
   Filevalidation = () => {
       const fi = document.getElementById('file');
       // Check if any file is selected.
       if (fi.files.length > 0) {
           for (const i = 0; i <= fi.files.length - 1; i++) {
 
               const fsize = fi.files.item(i).size;
               const file = Math.round((fsize / 1024));
               // The size of the file.
               if (file >= 4096) {
                   alert(
                     "File too Big, please select a file less than 4mb");
               } else if (file < 2048) {
                   alert(
                     "File too small, please select a file greater than 2mb");
               } else {
                   document.getElementById('size').innerHTML = '<b>'
                   + file + '</b> KB';
               }
           }
       }
   }
</script>
 
</html>
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!