0

UPDATE Slowly by slowly i progress, so i change my mind for use append() method. My ajax request is ok :

      var arrImg = new FormData();
  for (var i = 0; i < nbFiles; i++) {
    var actualName = $(fromInput)[0].files[i].name;
    if(actualName != valueToDelete){
      imgValue = document.getElementById('image_1').files[i];
      arrImg.append("image[]", imgValue);
    }
  }
  arrImg.append('action','delete_tof');
  arrImg.entries();
  // console.log(arrImg)
  my_url = ajaxurl;
  $.ajax({
    url:my_url,
    data :arrImg,
    contentType: false,
    processData: false,
    method: 'POST',
    success : function(data){ 
      console.log(data);
    },
    error : function( data ) {
        console.log(data)
        alert('Une erreure s\'produite, au moment d\'effacer votre image');
        // location.reload();
    }
  }) 

result of the consol.log(data) The result of ajax request is ok

Then my function php

function delete_tof(){
// Count total files
$countfiles = count($_FILES['image']['name']);

// Upload directory
$upload_location = '/tmp/';

// To store uploaded files path
$files_arr = array();

// Loop all files
for($index = 0;$index < $countfiles;$index++){

   // File name
   $filename = $_FILES['image']['name'][$index];

   // Get extension
   $ext = pathinfo($filename, PATHINFO_EXTENSION);

   // Valid image extension
   $valid_ext = array("png","jpeg","jpg");

   // Check extension
   if(in_array($ext, $valid_ext)){

     // File path
     $path = $upload_location.$filename;

     // Upload file
     if(move_uploaded_file($_FILES['image']['tmp_name'][$index],$path)){
        $files_arr[] = $path;
     }
   }

}
echo json_encode($files_arr);
wp_die(); 

}

And the the vardump of $_FILES and $files_arr is ok

the vardump of $_FILES in function is ok

But i don't understand why, when i submit my original form i still have the wrong number of files ?

Next the first attempt but Filelist is not updatable so i left this try

i'm trying to update Filelist in javascript i create array of datas image like that

  var arrImg = [];
  for (var i = 0; i < nbFiles; i++) {
      imgValue = $(fromInput)[0].files[i];
      arrImg.push(imgValue);
  }

With this loop i have what i need for update FileList.

My array of datas

The probleme after is for update Filelist.

I've found that :

Function for update list

But I can't adapt it to my problem. I've trying different stuff like

arrImg.push(new File(imgValue)); //In the loop 

then

$(fromInput).arrImg = FileListItem(arrImg)

But no success because an argument is missing

Maybe there is another way with file() method but that doesn't work as well

$(fromInput).file(['File'],arrImg[1])
laurent
  • 67
  • 6
  • FileList is not constructable or changeable, so you only can use workarounds which are not working in all browsers (e.g. Safari or some moble ones). So your best option is take snippets proven to work (see your provided links) – Codebreaker007 Mar 22 '20 at 10:19
  • ok, mayby there is a way just for delete a row in filelist ? – laurent Mar 22 '20 at 11:01
  • Wether you add or you delete - this is always by definition an update. Why not create it new and display it then? – Codebreaker007 Mar 22 '20 at 11:03
  • You mean, remove my in the dom and insert new one with value what i want ? – laurent Mar 22 '20 at 12:29

0 Answers0