Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the size of a file upload (html input element)

Tags:

html

I would like to simply limit the size of a file that a user can upload.

I thought maxlength = 20000 = 20k but that doesn't seem to work at all.

I am running on Rails, not PHP, but was thinking it'd be much simpler to do it client side in the HTML/CSS, or as a last resort using jQuery. This is so basic though that there must be some HTML tag I am missing or not aware of.

Looking to support IE7+, Chrome, FF3.6+. I suppose I could get away with just supporting IE8+ if necessary.

Thanks.

like image 963
delphi Avatar asked Apr 18 '11 01:04

delphi


People also ask

How do I limit the upload file size in HTML?

size > 2097152){ alert("File is too big!"); this. value = ""; }; }; This example should work fine. I set it up for roughly 2MB, 1MB in Bytes is 1,048,576 so you can multiply it by the limit you need.

How do you limit the maximum file selected when using multiple inputs?

Use two <input type=file> elements instead, without the multiple attribute. If the requirement is really just "have two different single inputs", then this is actually the best answer @Diego.

How do I limit file size?

Remove unnecessary images, formatting and macros. Save the file as a recent Word version. Reduce the file size of the images before they are added to the document. If it is still too large, save the file as a PDF.

How do I restrict multiple file uploads in HTML?

To allow multiple file uploads in HTML forms, use the multiple attributes. The multiple attributes work with email and file input types. For limiting maximum items on multiple inputs, use JavaScript. Through this, limit the number of files to be uploaded.


2 Answers

var uploadField = document.getElementById("file");  uploadField.onchange = function() {     if(this.files[0].size > 2097152){        alert("File is too big!");        this.value = "";     }; }; 

This example should work fine. I set it up for roughly 2MB, 1MB in Bytes is 1,048,576 so you can multiply it by the limit you need.

Here is the jsfiddle example for more clearence:
https://jsfiddle.net/7bjfr/808/

like image 26
Haseeb Rehman Avatar answered Sep 21 '22 09:09

Haseeb Rehman


This is completely possible. Use Javascript.

I use jQuery to select the input element. I have it set up with an on change event.

$("#aFile_upload").on("change", function (e) {      var count=1;     var files = e.currentTarget.files; // puts all files into an array      // call them as such; files[0].size will get you the file size of the 0th file     for (var x in files) {          var filesize = ((files[x].size/1024)/1024).toFixed(4); // MB          if (files[x].name != "item" && typeof files[x].name != "undefined" && filesize <= 10) {               if (count > 1) {                  approvedHTML += ", "+files[x].name;             }             else {                  approvedHTML += files[x].name;             }              count++;         }     }     $("#approvedFiles").val(approvedHTML);  }); 

The code above saves all the file names that I deem worthy of persisting to the submission page, before the submit actually happens. I add the "approved" files to an input element's val using jQuery so a form submit will send the names of the files I want to save. All the files will be submitted, however, now on the server side we do have to filter these out. I haven't written any code for that yet, but use your imagination. I assume one can accomplish this by a for loop and matching the names sent over from the input field and match them to the $_FILES(PHP Superglobal, sorry I dont know ruby file variable) variable.

My point is you can do checks for files before submission. I do this and then output it to the user before he/she submits the form, to let them know what they are uploading to my site. Anything that doesn't meet the criteria does not get displayed back to the user and therefore they should know, that the files that are too large wont be saved. This should work on all browsers because I'm not using FormData object.

like image 120
mark.inman Avatar answered Sep 22 '22 09:09

mark.inman