我想询问如何在上传之前删除文件图像的值。我的问题是当我删除一个文件图像时,输入类型文件没有删除值,之后我按下按钮发送到php代码它仍然显示所有文件。
window.onload = function(){
//Check File API support
if(window.File && window.FileList && window.FileReader)
{
var filesInput = document.getElementById("files");
filesInput.addEventListener("change", function(event){
var files = event.target.files; //FileList object
var output = document.getElementById("result");
for(var i = 0; i< files.length; i++)
{
var file = files[i];
//Only pics
if(!file.type.match('image'))
continue;
var picReader = new FileReader();
picReader.addEventListener("load",function(event){
var picFile = event.target;
var div = document.createElement("div");
div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
"title='" + picFile.name + "'/> <a href='#' class='remove_pict'>X</a>";
output.insertBefore(div,null);
div.children[1].addEventListener("click", function(event){
div.parentNode.removeChild(div);
});
});
//Read the image
picReader.readAsDataURL(file);
}
});
}
else
{
console.log("Your browser does not support File API");
}
}
body{
font-family: 'Segoe UI';
font-size: 12pt;
}
h1{
font-size:12pt;
color: #fff;
background-color: #1BA1E2;
padding: 20px;
}
article
{
width: 80%;
margin:auto;
margin-top:10px;
}
.thumbnail{
height: 100px;
margin: 10px;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>More file uploads</title>
</head>
<body>
<h1>Select multiple files</h1>
<article>
<form action="#" method="post" enctype="multipart/form-data">
<input id="files" type="file" name="files[]" multiple/>
<output id="result" />
<input type="submit" name="btnsubmit" value="Send"/>
</form>
</article>
<?php
if(isset($_POST['btnsubmit'])){
var_dump($_FILES['files']['name']);
}
?>
</body>
</html>
答案 0 :(得分:0)
我见过您的代码,我认为代码打击只是删除了div,但没有更改$_FILES
div.children[1].addEventListener("click", function(event){
div.parentNode.removeChild(div);
});