• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Finding file size using javascript

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript can not do that for security reasons.

Eric
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this way :

 
Ranch Hand
Posts: 82
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ishan Shah
Ranch Hand
Posts: 82
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you wont to find size of file in JavaScript than you use the
Following command
=>   document.getElementById(‘file’).files[0].size;
document.getElementById(‘file’).files[0].name;
document.getElementById(‘file’).files[0].type;

You can also find the name of the file and type of file with help of bellow command
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic