简体   繁体   English

如何控制多个上传计数5

[英]how to control multiple uploads to count 5

I want to upload images for adding property and have some code in which i am able to add more than 5 file uploads. 我想上传图像以添加属性,并有一些代码,我可以添加超过5个文件上传。 How can stop the uploads at five. 如何在五点停止上传。 here is the sample code i am using 这是我正在使用的示例代码

<form enctype="multipart/form-data" action="" method="post">
<div id="filediv"><input name="file[]" type="file" id="file"/></div><br/>
<input type="button" id="add_more" class="upload" value="Add More Files"/>
<input type="submit" value="Upload File" name="submit" id="upload" class="upload"/>
</form>

and the js code for add button is 并且添加按钮的js代码是

$('#add_more').click(function() {
$(this).before($("<div/>", {id: 'filediv'}).fadeIn('slow').append(
$("<input/>", {name: 'file[]', type: 'file', id: 'file'}),        
$("<br/><br/>")
));
});

how can i stop add more button can be available for only five. 我怎么能停止添加更多按钮,只有五个可用。

Before adding the input, check if there are 5 in de div with id filediv. 在添加输入之前,检查de div中是否有5个带有id的文件。 So yes, stop with return. 所以是的,停下来回来。

I've changed and simplified your code, so the input fields will be added and count in one div (div#filediv). 我已经更改并简化了代码,因此输入字段将被添加并计入一个div(div#filediv)。

$('#add_more').click(function() {
   if ($('div#filediv').find('input[type="file"]').length >= 5)
      return;
   $('div#filediv').append(
      $('<input/>', {name: 'file[]', type: 'file', id: 'mustBeUniquePerPage'})
   );
});

Make sure that you use unique id's per page! 确保每页使用唯一的ID!

More efficient solution, less DOM and memory intensive: 更有效的解决方案,更少的DOM和内存密集型:

var totalFilesAdded = 0;
$('#add_more').on('click', function() {
   if (totalFilesAdded >= 5)
      return;
   $('div#filediv').append(
      $('<input/>', {name: 'file[]', type: 'file', id: 'mustBeUniquePerPage'})
   );
   totalFilesAdded++;
});

Read here more why you better use .on('click') vs .click(): Difference between .on('click') vs .click() 在这里阅读更多为什么你更好地使用.on('click')vs .click():.on('click')vs .click(之间的区别

The most important consideration here is that you can only really restrict the number of file uploads on the server. 这里最重要的考虑因素是您只能真正限制服务器上的文件上传次数。 No Javascript or HTML will ever prevent users uploading more than 5 files. 没有Javascript或HTML会阻止用户上传超过5个文件。 You need to set limits on the server. 您需要在服务器上设置限制。

The max_file_uploads PHP.ini directive allows you to limit the number of files sent via a POST request to the PHP file processing uploads. max_file_uploads PHP.ini指令允许您将通过POST请求发送的文件数限制为PHP文件处理上载。 Even this will only restrict how many files are uploaded in a single POST request. 即使这样也只会限制在单个POST请求中上传的文件数量。 You also need to track in your database/file system how many have been uploaded already and check these limits are not breached. 您还需要在数据库/文件系统中跟踪已上载的数量,并检查这些限制是否未被破坏。

The name attribute you have give to your input element ( file[] ) implies the use of a multiple upload facility, but your HTML does not have the multiple attribute. 您为input元素( file[] )提供的name属性意味着使用多个上载工具,但您的HTML没有multiple属性。 You can either have a multiple file upload in which users can upload all 5 images at once, or keep doing it as you are and check how many input fields have been generated (I won't post code for this as another answer already has). 您可以使用多个文件上传,用户可以一次上传所有5个图像,或者按原样继续操作并检查已生成的input字段数(我不会为此发布代码,因为另一个答案已经存在) 。

Either way please remember that you will still need to check how many files have already been uploaded, as everything in HTML and Javascript can be bypassed. 无论哪种方式,请记住,您仍然需要检查已经上传了多少文件,因为可以绕过HTML和Javascript中的所有内容。

You can use global variable as a counter and initialize it to 0. On click increment the counter. 您可以将全局变量用作计数器并将其初始化为0.单击会增加计数器。 Before adding any property just check if count is less than 5. 在添加任何属性之前,只需检查count是否小于5。

var counter = 0;
$('#add_more').click(function() {
counter = counter+1;
if(counter<=5){
$(this).before(...)
}
)};

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

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