0

My Drupal version is 8.6.10

I am trying to upload a file through the file_save_upload function and the function is looking for the file in

Symfony\Component\HttpFoundation\FileBag Object

$all_files = \Drupal::request()->files;
debug($all_files);

Symfony\Component\HttpFoundation\FileBag Object
(
    [parameters:protected] => Array
        (
        )

)

But my file is in

Symfony\Component\HttpFoundation\ParameterBag Object.

$all_files = \Drupal::request()->request;
debug($all_files);

Symfony\Component\HttpFoundation\ParameterBag Object
(
    [parameters:protected] => Array
        (
            [name] => 
            [type] => 4
            [files] => Array
                (
                    [image_section] => logo_capnord.png
                )

            [op] => Ajouter
            [form_build_id] => form-wMTrCGaN5mPlOOv8bqS7UsJ0lRiSN6QQp9UviA0cTIA
            [form_token] => 1hTMXJRfoQvFV9Lqq3TuJ29eYC6L-zt3U7Bow1P8yJ0
            [form_id] => capnord_section_form_list
        )

)

The field is generated through the reloading of the form in ajax.

  $form["base"]["fields"] += [
    "image_section" => [
      "#type" => "file",
      "#title" => "Image",
    ]
  ];
3
  • I solved my problem, as I only reloaded one part of the form, the form generator did not know that there was a file element, so it did not add "multipart enctype / form-data" to the form. Mar 14, 2019 at 14:30
  • 1
    If you found a solution please enter it in as an answer
    – Matt
    Mar 14, 2019 at 18:12
  • 1
    I didn't know that i can answer to myself, thanks Mar 15, 2019 at 14:06

1 Answer 1

0

Solution

$form["#attributes"] = ["enctype" => "multipart/form-data"];

$form["base"]["fields"] += [
   "image_section" => [
    "#type" => "file",
    "#title" => "Image",
   ]
];

I solved my problem, as I only reloaded one part of the form, the form generator did not know that there was a file element, so it did not add "multipart enctype / form-data" to the form.

I hope this will help someone in the same situation as me in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.