Search code examples
javascriptlaravelformsdata-transferfilelist

request getting the last file in form has inputs filled by datatransfer, laravel


first of all, I don't know if it's legal to do this, but please read this question to understand me better.

as you read in the question, I have trouble pushing new files into an input, I knew that I get only the last file because of pushing the files into input's file list. so I changed pushing from this: (which was working, but only on last input in the form)

function inputImage(quode, f){
    let list = new DataTransfer();
    list.items.add(f);
    let img = list.files;

    document.querySelector("#imageUploader_"+quode).files = img;
}

to this:

function inputImage(quode, f){
   document.querySelector("#imageUploader_"+quode).files.push(f);
}

but I get this error

document.querySelector(...).files.push is not a function

I get it from this answer

I've also tried this

function inputImage(quode, f){
    document.querySelector("#imageUploader_"+quode).files.add(f);
}

but I get this

TypeError: document.querySelector(...).files.add is not a function

the file object looks like (from the console):

File {
  lastModified: 1642515255138
  lastModifiedDate: Tue Jan 18 2022 17:14:15 GMT+0300 (GMT+03:00) {}
  name: "[(33),12,2314,100,45].jpeg"
  size: 8115
  type: "image/jpeg"
  webkitRelativePath: ""
  [[Prototype]]: File
}

the file list :

FileList {length: 0[[Prototype]]: FileList}

EDIT: see this it may help

and this is some documentation:

  1. Mozilla datatransfer
  2. Mozilla FileList

Solution

  • the reason wasn't related to datatransfer even nor related to JS. the problem that I wasn't managing the request array perfectly, so I only get the last input's file, so I changed the request from this

    public function store(Request $request){
            $gen = ['_token','examHash','name','duration','bornline','deadline','note','hardness','vip','examType','published'];
            $questions = $request->except($gen);//questions
            $data = [];
            $count = 0;
            foreach($questions as $k => $i){
                if(str_starts_with($k, 'ex_')){
                    $quode= '';
                    $ex = '';
                    $type = '';
                    $quode = str_replace("ex_", "", $k);
                    $ex = $i;
                    $type = $questions['questionType_'.$quode];
                    $count += 1;
                }
                $data += [$quode => []];
                if(str_ends_with($k, $quode)){
                    // check if the $k is unnecessary element and skip it, add only needed data to $data array
                    if((($type == "option" && (!str_starts_with($k, "label") && !str_starts_with($k, "single") && !str_starts_with($k, "choice"))) || ($type == "label" && (!str_starts_with($k, "option") && !str_starts_with($k, "single"))) || ($type == "single" && (!str_starts_with($k, "label") && !str_starts_with($k, "option") && !str_starts_with($k, "choice"))))){
                        $data[$quode]['sort'] = $count;
                        $data[$quode] += [str_replace("_".$quode, "", $k) => $i];
                    }
                    if(str_starts_with($k, "image")){
                        $questions['image_'.$quode]->storeAs('ques', $quode.$ex);
                    }
                }
            }
            $data = array_merge(request()->only($gen), $data);
            dd($data);
        }
    

    to this

    foreach($questions as $k => $i){
        if(str_starts_with($k, 'ex_')){
            $quode= '';
            $ex = '';
            $type = '';
            $image= [''];
            $quode = str_replace("ex_", "", $k);
            $ex = $i;
            $type = $questions['questionType_'.$quode];
            $image = $questions['image_'.$quode];
            $count += 1;
        }
        $data += [$quode => []];
        if(str_ends_with($k, $quode)){
            // check if the $k is unnecessary element and skip it, add only needed data to $data array
            if((($type == "option" && (!str_starts_with($k, "label") && !str_starts_with($k, "single") && !str_starts_with($k, "choice"))) || ($type == "label" && (!str_starts_with($k, "option") && !str_starts_with($k, "single"))) || ($type == "single" && (!str_starts_with($k, "label") && !str_starts_with($k, "option") && !str_starts_with($k, "choice"))))){
                $data[$quode]['sort'] = $count;
                $data[$quode] += [str_replace("_".$quode, "", $k) => $i];
                $data[$quode]['image'] = $image;
                $data[$quode]['image']->storeAs('ques', $quode.$ex);
            }
        }
    }