jeudi 4 avril 2019

Appended files to FormData not received at Laravel server

I'm facing a strange issue ... I've a form with multiple fields .. on form submit, I append multiple image files to form data and submit it like this:

            $('#newEntry').on('submit', function(e){
                e.preventDefault();
                const formData = new FormData(e.target);

                formData.delete('images[]');

                for (var i = 0; i < filesToUpload.length; i++) {
                    formData.append('images[]', filesToUpload[i].file, filesToUpload[i].file.name);
                }
                $(this)[0].submit();
            });

and when I try to echo(count($request->images)) at Laravel server it echos 0 .. and on dd($request) .. I see empty files array

but when I submit same form with same files directly from input field instead of appending data to it like this:

<input type="file" name="images[]">

I receive all files at server.

Files are successfully appended to formData .. I've checked it by:

                var formKeys    = formData.keys();
                var formEntries = formData.entries();

                do {
                  console.log(formEntries.next().value);
                } while (!formKeys.next().done)

I've also tried to send same appended files through AJAX and it worked perfectly fine:

             $('#newEntry').on('submit', function(e){
                e.preventDefault();
                const formData = new FormData(e.target);

                formData.delete('images[]');

                for (var i = 0; i < filesToUpload.length; i++) {
                    formData.append('images[]', filesToUpload[i].file, filesToUpload[i].file.name);
                }

                $.ajax({
                    url: "/admin/p/products/new",
                    data: formData,
                    processData: false,
                    contentType: false,
                    type: "POST",
                    success: function (data) {
                        alert("DONE");

                        // files1Uploader.clear();
                        // files2Uploader.clear();
                        // files3Uploader.clear();
                    },
                    error: function (data) {
                        alert("ERROR - " + data.responseText);
                    }
                });
             });

on digging down deeper .. I've found that when I submit a form via Http request an Error with code 500 Internal Server Error appears in console for a moment (just before page reloads)

tried everything but don't know what is causing this strange behavior .. kindly help me sort it out



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire