Rit*_*apa 2 html javascript forms input filelist
我有以下代码用于多个文件输入
<form action="" enctype = "multipart/form-data" method="post" name="login">
<input type = "file" name = "photo[]" id = "files" multiple onchange = "handleFileSelect(this.files)"/><br/>
<div id="selectedFiles"></div>
<input type="submit" value="Sign In">
</form>
Run Code Online (Sandbox Code Playgroud)
javascript的等效功能是。
selDiv = document.querySelector("#selectedFiles");
function handleFileSelect(e) {
if(!this.files) return;
selDiv.innerHTML = "";
var files = e;
for(var i=0; i<files.length; i++) {
var f = files[i];
selDiv.innerHTML += f.name + "<br/>";
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的是上传第二个文件。FileList被覆盖,第二个文件出现在FileList中,而不是2个文件。这里FileList由this.files传递。
同样在传递到服务器时,仅传递第二张图像。我已经在Google上搜索过,但是找不到答案。如果有人可以帮助,我将不胜感激。
实际上,这就是带有multiple属性的HTML文件输入的工作方式。用户必须使用Shift或Control Click一次选择他们要上传的所有文件。
为了让您的站点用户多次使用HTML文件输入元素并保留所有先前的选择,您需要在每次使用file元素时将接收到的base64数据写入隐藏的表单元素。
<form action="process.php" method="post" name="uploadform" enctype="multipart/form-data">
// other form elements if needed
<input type="submit">
</form>
<!-- outside the form, you don't want to upload this one -->
<input type="file" id="upfiles" name="upfiles">
<script>
document.getElementById('upfiles').addEventListener('change', handle_files, false);
function handle_files(evt) {
var ff = document.forms['uploadform'];
var files = evt.target.files;
for ( var i = 0, file; file = files[i]; i++ ) {
var reader = new FileReader();
reader.onload = (function(file) {
return function (ufile) {
var upp = document.createElement('input');
upp['type'] = 'hidden';
upp['name'] = +new Date + '_upfile_' + file.name.replace(/(\[|\]|&|~|!|\(|\)|#|\|\/)/ig, '');
upp.value = ufile.target.result;
ff.appendChild(upp);
}
}(file));
reader.readAsDataURL(file);
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
接下来,您需要编写脚本以在服务器上运行以处理隐藏的base64字段。如果使用PHP,则可以:
<?php
$path = 'path/to/file/directory/';
// this is either:
// - the absolute path, which is from server root
// to the files directory, or
// - the relative path, which is from the directory
// the PHP script is in to the files directory
foreach ( $_POST as $key => $value ) { // loop over posted form vars
if ( strpos($key, '_upfile_') ) { // find the file upload vars
$value = str_replace(' ', '+', $value); // url encode
file_put_contents($path.$key, base64_decode($value));
// convert data to file in files directory with upload name ($key)
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题。感谢您的提问和回答。我设法通过添加到DOM输入类型文件中并将单击委托给分离的元素来添加了几个文件:
<form method="POST" enctype="multipart/form-data" action="/echo/html">
<button class="add">
Add File
</button>
<ul class="list">
</ul>
<button>
Send Form
</button>
</form>
Run Code Online (Sandbox Code Playgroud)
使用javascript:
$('form button.add').click(function(e) {
e.preventDefault();
var nb_attachments = $('form input').length;
var $input = $('<input type="file" name=attachment-' + nb_attachments + '>');
$input.on('change', function(evt) {
var f = evt.target.files[0];
$('form').append($(this));
$('ul.list').append('<li class="item">'+f.name+'('+f.size+')</li>');
});
$input.hide();
$input.trigger('click');
});
Run Code Online (Sandbox Code Playgroud)
它可以与Edge,Chrome 50和firefox 45一起使用,但是我不知道与旧版本或其他浏览器的兼容性。
看到这个小提琴。