Home > Enterprise >  Issue in uploading multiple files and only 1 file to firebase storage
Issue in uploading multiple files and only 1 file to firebase storage

Time:03-15

I can successfully upload multiple files to firebase storage the issue is when I try to upload only 1 file nothing happened.

When I have multiple files they are added to an array then I can upload it to firebase database but how to do it with only 1 file.

//choose files
  document.querySelector('#pictures').addEventListener('change', (e) => {

    const formData = extractFormData('#statusForm');
    while (fileCollection.length) {
      fileCollection.pop();
      console.log("test");

    }
    [].slice.call(formData.pictures).map(f => fileCollection.push(f));
    console.log(formData.pictures);

        
  });
    //choose files
  document.forms.statusForm.addEventListener('submit', (e) => {
    e.preventDefault();
    e.stopPropagation();

    const formData = extractFormData('#statusForm');
    const text = formData.status;
    formData.status = '';
    sendData(text, fileCollection)
    setTimeout(() => {
      while (fileCollection.length) {
        fileCollection.pop();
      }
    }, 100);
  }); 


var extractFormData = function (form) {
  const formData = new FormData(document.querySelector(form));
  values = {};


  for(var pair of formData.entries()) {
    if( values[pair[0]] ) {

      if(!(values[pair[0]] instanceof Array)) {
        values[pair[0]] = new Array(values[pair[0]]);

      }
      values[pair[0]].push(pair[1]);


    } else {

      values[pair[0]] = pair[1];

    }
  }

  console.log("values: "   values);

  return values;
}

CodePudding user response:

This is how it was fixed i changed the extractFormData function to

const extractFormData = function (form) {
  const formData = new FormData(document.querySelector(form));
  const values = {};
  for (let [key, value] of formData.entries()) {
    
    if (values[key]) {
      if ( ! (values[key] instanceof Array) ) {
        values[key] = new Array(values[key]);
      }
      values[key].push(value);

    } else {
      values[key] = value;
      if(typeof values[key] === 'undefined') {
        // does not exist
        console.log("doesnt exist")
    }
    else {
        // does exist
        values[key] = new Array(values[key]);
    }

    }
  }
  return values;
}
  • Related