如何防止删除图像时出现“未找到您的文件”消息?Javascript

如何防止删除图像时出现“未找到您的文件”消息?Javascript,javascript,Javascript,当我试图删除我得到的图像时,你的文件没有找到,我正在使用谷歌浏览器 HTML: 我尝试过使用Filereader。我知道有一种方法可以使用splice,但无法找到解决方案。选择的图像作为预览显示,我希望用户能够使用javascript或jquery删除不需要的图像 使用PHP删除您的图像,Javascript不会这样做: <form action="upload_file.php" method="post" enctype="multipart/form-data"> <in

当我试图删除我得到的图像时,你的文件没有找到,我正在使用谷歌浏览器

HTML:

我尝试过使用Filereader。我知道有一种方法可以使用splice,但无法找到解决方案。选择的图像作为预览显示,我希望用户能够使用javascript或jquery删除不需要的图像

使用PHP删除您的图像,Javascript不会这样做:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" id="upload_file" name="file" onchange="preview_image()" 
multiple/> 
</form>

<script>
function preview_image() 
{
 var total_file=document.getElementById("upload_file").files.length;
 for(var i=0;i<total_file;i++)
{
 $('#demo').append("<img id='pre' alt='image' 
 src='"+URL.createObjectURL(event.target.files[i])+"'><br><button 
 id='del' data-img='"+URL.createObjectURL(event.target.files[i])+"'>Delete</button><br>");
 }
 }

// Delete using jquery 
$(document).ready(function(){
$(document).on('click','button',function(event){

$.post("yourcode.php", {img: $(this).data('img')}, function(data, status){
       if (data == '1'){
        alert("IMG DELETED");
        $(this).hide().prev().remove();
       } else {
          alert('ERROR DELETING IMAGE!');
       }
    });

})
});
</script>
您的php代码:

<?php
if (isset($_POST['img'])){
  if (unlink($_POST['img'])) {
      echo json_encode('1'); //true
  } else {
      echo json_encode('0'); //false
  }
}

$pre.remove?$pre删除所有图像OK,您应该附加一个IDF,因为某些原因我发布的代码可以在线工作,但我在脱机工作时收到一条错误消息嗯。。。也许是缓存?关闭php.ini文件中的opcache
<?php
if (isset($_POST['img'])){
  if (unlink($_POST['img'])) {
      echo json_encode('1'); //true
  } else {
      echo json_encode('0'); //false
  }
}