动态创建多个上传文件 [英] Create multiple Upload File dynamically

查看:60
本文介绍了动态创建多个上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人知道动态创建上传表单的最佳方式?



这是我想要实现的:下面显示的代码允许一个上传,我想要一个按钮,当按下时,应该为文件上传添加另一个表单。所以,如果我想上传 - 比方说7个文件,我想按下按钮7次来创建这些上传表格,每个都在它自己的行上。



无论如何,我可以做到这一点?



感谢您的协助:

  < HTML> 
< head>
< title>多个文件上传< / title>
< / head>
< body>
< form enctype =multipart / form-dataaction =uploader.phpmethod =POST>
选择要上传的文件:< input name =uploadedfiletype =file/>< br />
< input type =submitvalue =Upload File/>
< / form>
< / body>
< / html>


解决方案

通常你会这样做,客户端:

 < div id ='Uploadcontainer'> 
< input type ='file'name ='uploadfiles []'class ='uploadfile'/>
< / div>
< button id ='extraUpload'>新增另一个栏位< / button>
< script type ='text / javascript'>
$('#extraUpload')。click(function(){
$('。uploadfile:last')。clone()。appendTo('#uploadContainer');
}) ;
< / script>

这就是使用jQuery。然后在服务器端,您可以轻松地遍历$ _FILES ['uploadfiles']数组:
$ b $ pre $ foreach($ _ FILES [''上传文件']作为$文件){
//做$文件
的东西}


I was wondering if anyone knew the best way to dynamically create an upload form?

Here's what I'm trying to achieve: The code shown below allows one upload, I want to have a button that when pressed, should add another form for file upload. So, if I want to upload - let's say 7 files, I want to press the button 7 times to create those upload forms, each on it's own row.

Is there anyway I can do it?

Thanks for your assistance:

<html>
    <head>
        <title> Multiple File Uploads </title>
    </head>
    <body>
        <form enctype="multipart/form-data" action="uploader.php" method="POST">
            Choose a file to upload: <input name="uploadedfile" type="file" /><br />
            <input type="submit" value="Upload File" />
        </form>
    </body>
</html>

解决方案

Usually you do something like this, Client-side:

<div id='Uploadcontainer'>
   <input type='file' name='uploadfiles[]' class='uploadfile' />
</div>
<button id='extraUpload'>Add another field</button>
<script type='text/javascript'>
  $('#extraUpload').click(function(){
      $('.uploadfile:last').clone().appendTo('#uploadContainer');
  });
</script>

That is using jQuery. Then on the server side you can easy loop over the $_FILES['uploadfiles'] array:

foreach($_FILES['uploadfiles'] as $file){
  //do stuff with $file
}

这篇关于动态创建多个上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆