获取表单中最后一个文件的请求具有由数据传输填充的输入,laravel

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

首先,我不知道这样做是否合法,但请阅读this question以更好地理解我。

正如您在问题中所读到的,我无法将新文件推送到输入中,我知道我只能得到最后一个文件,因为 将文件推送 到输入的文件列表中.所以我从这个改变了推动:(这是有效的,但仅在表单中的最后输入

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

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

对此:

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

但是我得到这个错误

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

我从

得到

我也试过这个

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

但我明白了

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

文件对象看起来像(从控制台):

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
}

文件列表:

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

编辑: 看看 它可能有帮助

这是一些文档:

  1. Mozilla datatransfer
  2. Mozilla FileList

原因与数据传输无关,甚至与 JS 无关。我没有完美管理请求数组的问题,所以我只得到最后一个输入的文件,所以我从这个

更改了请求
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);
    }

至此

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);
        }
    }
}