简体   繁体   English

jQuery:在表单中动态添加文件上传字段并验证总大小

[英]jQuery: Dynamically add file upload fields in form and validate total size

Is it possible to use jQuery to calculate/validate/restrict total upload size of dynamically added file fields? 是否可以使用jQuery计算/验证/限制动态添加的文件字段的总上传大小? The form will be sending the content as an email copy, so it should be limiting the total upload size to eg. 表单将以电子邮件副本的形式发送内容,因此应将上传的总大小限制为例如。 20MB or so. 20MB左右。 The backend validation is no problem (by PHP). 后端验证没有问题(通过PHP)。

The validation could either be done 'on the fly' or when hitting submit. 验证可以“即时”完成,也可以在提交时完成。

Consider the following code: 考虑以下代码:

<div class="input_fields_wrap">
  <a href="#" class="add_field_button">+ Add</a>
  <div>
    <input type="file" name="attachment[]">
  </div>
</div>

And the jQuery: 和jQuery:

$(document).ready(function() {
  var max_fields = 10;
  var wrapper = $(".input_fields_wrap");
  var add_button = $(".add_field_button");

  var x = 1;
  $(add_button).click(function(e) {
    e.preventDefault();
    if (x < max_fields) {
      x++;
      $(wrapper).append('<div><input type="file" name="attachment[]"/><a href="#" class="remove_field">Remove</a></div>');
    }
  });

  $(wrapper).on("click", ".remove_field", function(e) {
    e.preventDefault();
    $(this).parent('div').remove();
    x--;
  })
});

Fiddle: https://jsfiddle.net/yyqnwfb9/1/ 小提琴: https : //jsfiddle.net/yyqnwfb9/1/

Any input on how to solve this would be much appreciated. 任何有关如何解决此问题的意见将不胜感激。

Update: New Fiddle with implemented solution: https://jsfiddle.net/yyqnwfb9/2/ 更新:带有实施解决方案的新提琴: https : //jsfiddle.net/yyqnwfb9/2/

You can use the HTML5 File API to get the size. 您可以使用HTML5 File API来获取大小。 For example, to get total size in bytes of all files in the page: 例如,要获取页面中所有文件的总大小(以字节为单位):

 function getTotal() { var total = 0; $(':file').each(function() { if (this.files && this.files[0]) { total += this.files[0].size; } }); return total; } $(function() { var maxSize = 3 * 1000 * 1000 ; // ~3MB $(document).on('change', ':file', function() { if (getTotal() > maxSize) { // Clear field $(this).val(''); // Display error? $('#total').append(' Too big'); } else { // Update total $('#total').text(getTotal()); } }); }); 
 input { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" name="attachment[]"> <input type="file" name="attachment[]"> <input type="file" name="attachment[]"> <input type="file" name="attachment[]"> <input type="file" name="attachment[]"> <strong>Total Bytes: <span id="total">0</span></strong> 

i have set file size limit of 3MB in upload function 我在上传功能中设置了3MB的文件大小限制

 $(document).ready(function() { var max_fields = 10; var wrapper = $(".input_fields_wrap"); var add_button = $(".add_field_button"); var x = 1; var uploadField = document.getElementById("file"); uploadField.onchange = function() { if(this.files[0].size > 307200){ alert("File is too big!"); this.value = ""; } else{ $(add_button).click(function(e) { e.preventDefault(); if (x < max_fields) { x++; $(wrapper).append('<div><input type="file" name="attachment[]"/><a href="#" class="remove_field">Remove</a></div>'); } }); $(wrapper).on("click", ".remove_field", function(e) { e.preventDefault(); $(this).parent('div').remove(); x--; }) } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="input_fields_wrap"> <a href="#" class="add_field_button">+ Add</a> <div> <input type="file" id="file" name="attachment[]"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM