1

I know this question is frequent but I can't find the right solution reading all the posts about Fetch and formData :

I have a lot of pseudo-forms written in JS in different divs to load images using a unique common function

 function uploadImage(ref,rep,int,num,typ,div){  
    
 let form = '<span class="uploader">';
     form+= '<input id="rep-'+num+'" value="'+rep+'"><br>'; // upload directory
     form+= '<input id="ref-'+num+'" value="'+ref+'"><br>'; // image name to use
     form+= '<input id="typ-'+num+'" value="'+typ+'"><br>'; // type of image
     form+= '<input id="div-'+num+'" value="'+div+'"><br>'; // where to display it once loaded on server
     form+= '<input type="file" class="fileimg" id="file-'+num+'">';
     form+= '<button onclick="saveImage('+num+')">'+int+'</button>'; 
     form+= '</span>';
    
     return form
 }

the code calling the function for 1st picture:

 document.getElementById('img-1').innerHTML = uploadImage('name-of-the-image','dir-to-put-the-file/','ADD AN IMAGE HERE',1,0,'img-1'));  

and now the JS to fetch img to server:

 function saveImage(n) {

   let rep = document.getElementById('rep-'+n).value, // dir
       ref = document.getElementById('ref-'+n).value, // name
       typ = document.getElementById('typ-'+n).value, // type
       div = document.getElementById('div-'+n).value; // div.id

   const url = "upload-imgs.php?imgname="+ref+"&directory="+rep+"&typeimg="+typ
   const files = document.querySelector('[type=file]').files;
   formData = new FormData();

   for (let i = 0; i < files.length; i++) {
      let file = files[i];
      console.log('FILE = '+file.name);
      formData.append('image', file);
   }

   fetch(url, {
      method: 'POST',
      body: formData
   }).then(response => {
      if( response.status==200 && response.ok == true) {
        return response.text().then( text => displayIf(text) )
      }
   });

 }

PHP write the image with the right name at the right place dependind the right type of image (photo, graph, plan, ...) then return echo $dir.$new_name and the function displayIt(img) shows the new image in the right div

It works perfectly but only for the first image loaded. I think the prob is coming from formData which is to reset, but despite testing different possibilities I cant't find the right one:

    if( response.status==200 && response.ok == true) {
        return response.text().then( text => displayIt(text) )
        formData.delete('image'); // log :  unreachable code after return statement
    }

Adding formData.reset() instead, or in the displayIt(img) function, doesn't solve the prob: same log. Where am I wrong?

Wolden
  • 195
  • 1
  • 12
  • ...sorry its not a prob of several images in the same form as suggested above by the person who have closed my post, but how to send several images one by one using the same common form. – Wolden Jul 19 '21 at 13:49
  • solved : instead of `const files = document.querySelector('[type=file]').files;` (...the first one founded) I use `const files = getElementById('file-'+n).files;` – Wolden Jul 19 '21 at 13:59
  • thx for reopening ;-) – Wolden Jul 19 '21 at 14:26

0 Answers0