我有一个带有上传字段的表单,允许用户选择多个文件.但是,我需要能够允许用户从文件夹1中选择文件1,然后从文件夹2中选择文件2,依此类推.
目前,当用户从文件夹1中选择文件1然后点击"打开"时,选择窗口关闭(将用户留在我的表单上).然后,如果用户从文件夹2中选择文件2并点击"打开"按钮,则删除文件1,仅保留文件2.
基本上,用户无法选择多个文件,除非它们都位于同一位置.有没有办法让文件1在选择文件2后保持选中状态?
不,你不能。这是由操作系统定义的行为,并且它们之间可能有所不同。你无法精确地控制这些事情,你总是会担心会发生什么。
如果人们必须选择的文件夹数量非常小,您可以提供多个上传字段。
这个怎么样?
该解决方案使用HTML,jQuery/Javascript和PHP(用于服务器端处理此数据).这个想法是:1.)HTML表单:包含一个"浏览"按钮,允许用户选择多个文件(在一个目录中).2.)jQuery:在表单中创建一个新按钮的选项,允许用户选择多个文件(在不同的目录中 - 甚至实际上是同一个!),能够"无限地"创建新按钮.3.)PHP:作为奖励,我考虑了很好地打包服务器端处理数据.
以下是HTML表单的外观(我使用了可点击对象的find-icon,但您可以轻松地将其替换为您选择的图形).
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype='multipart/form-data'>
Select files: <br/>
<input type='file' name='files0[]' id="files0" multiple><br/><br/><br/>
<span style="font-size: 10pt;">Click "+" for more files
<i id="more_files" class="general foundicon-plus" style="color: blue;cursor: pointer;"></i></span>
<br/><br/><br/>
<input type="submit" name="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
这是触发事件后创建一个新的"浏览"按钮的jQuery/Javascript(这甚至将它放在最后的"浏览"按钮之后!):
<script type="text/javascript">
//jQuery
$(document).ready(function() {
$(document).on('click','#more_files', function() {
var numOfInputs = 1;
while($('#files'+numOfInputs).length) { numOfInputs++; }//once this loop breaks, numOfInputs is greater than the # of browse buttons
$("<input type='file' multiple/>")
.attr("id", "files"+numOfInputs)
.attr("name", "files"+numOfInputs+"[]")
.insertAfter("#files"+(numOfInputs-1));
$("<br/>").insertBefore("#files"+numOfInputs);
});
});
</script>
<script>
//vanilla javascript version
var location = document.getElementById("fileBrowsers");
var br = document.createElement("BR");
location.appendChild(br);
var input = document.createElement("input");
input.type = "file";
input.name = "files"+numOfInputs+"[]";
input.id = "files"+numOfInputs;
input.multiple = true;
location.appendChild(input);
</script>
Run Code Online (Sandbox Code Playgroud)
最后,也可能是最重要的,如何以熟悉的格式包装服务器上的数据:
<?php
if(isset($_POST['submit']) && !empty($_FILES)) {
$files = array();
$files = $_FILES['files0'];
//var_dump($files);//this array will match the structure of $_FILES['browser']
//Iterate through each browser button
$browserIterator = 1;
while(isset($_FILES['files'.$browserIterator])) {
//Files have same attribute structure, so grab each attribute and append data for each attribute from each file
foreach($_FILES['files'.$browserIterator] as $attr => $values) {//get each attribute
foreach($_FILES['files'.$browserIterator][$attr] as $fileValue) {//get each value from attribute
$files[$attr][] = $fileValue;//append value
}
}
$browserIterator++;
}
//Use $files like you would use $_FILES['browser'] -- It is as though all files came from one browser button!
$fileIterator = 0;
while($fileIterator < count($files['name'])) {
echo $files['name'][$fileIterator]."<br/>";
$fileIterator++;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
更新注意: jQuery脚本和vanilla Javascript实现了相同的目标.我遇到了一个需要vanilla版本的问题.你只需要其中一个.
| 归档时间: |
|
| 查看次数: |
6330 次 |
| 最近记录: |