Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file size before uploading

Is there any way to find out the file size before uploading the file using AJAX / PHP in change event of input file?

like image 593
Nisanth Kumar Avatar asked Sep 21 '11 09:09

Nisanth Kumar


People also ask

How do you show file size in HTML?

The id="size" element will be used to display the size of selected file from the id="upload" element.

How can I see how big a file will upload before Outsystems?

Regarding the file size validation, there is a BinaryDataSize action in BinaryData to get the file size.


2 Answers

For the HTML bellow

<input type="file" id="myFile" /> 

try the following:

//binds to onchange event of your input field $('#myFile').bind('change', function() {    //this.files[0].size gets the size of your file.   alert(this.files[0].size);  }); 

See following thread:

How to check file input size with jQuery?

like image 80
Brij Avatar answered Sep 28 '22 01:09

Brij


Here's a simple example of getting the size of a file before uploading. It's using jQuery to detect whenever the contents are added or changed, but you can still get files[0].size without using jQuery.

$(document).ready(function() {    $('#openFile').on('change', function(evt) {      console.log(this.files[0].size);    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <form action="upload.php" enctype="multipart/form-data" method="POST" id="uploadform">    <input id="openFile" name="img" type="file" />  </form>

Here's a more complete example, some proof of concept code to Drag and Drop files into FormData and upload via POST to a server. It includes a simple check for file size.

like image 31
jaggedsoft Avatar answered Sep 28 '22 01:09

jaggedsoft