0

There is a function that triggers the input file and shows previews:

$(document).on("change", "#Multifileupload", function() {
    var MultifileUpload = document.getElementById("Multifileupload");

    if (typeof FileReader != "undefined") {
        var MultidvPreview = document.getElementById("MultidvPreview");

        console.log(MultifileUpload.files);

        var images = Array.prototype.slice.call(
            MultifileUpload.files,
            0,
            upload_product_images_comment_total
        );

        for (
            var i = 0; i < images.length &&
            upload_product_images_comment_loaded <=
            upload_product_images_comment_total; i++
        ) {
            var file = images[i];
            var reader = new FileReader();

            reader.onload = function(e) {
                var img = document.createElement("img");
                var span = document.createElement("span");

                span.classList.add("remove_image");
                span.classList.add("icon-close");

                img.src = e.target.result;
                img.classList.add("Multifileupload_image");

                var position = upload_product_images_comment_loaded + 1;

                if (position > upload_product_images_comment_total - 1) {
                    var li = document.createElement("li");
                    $(".upload-photo-thumb").hide();
                    MultidvPreview.prepend(li);
                    MultidvPreview.children[0].appendChild(img);
                    MultidvPreview.children[0].appendChild(span);
                } else {
                    MultidvPreview.children[position].appendChild(img);
                    MultidvPreview.children[position].appendChild(span);
                }

                upload_product_images_comment_loaded++;
            };
            reader.readAsDataURL(file);
        }

        MultifileUpload.value = '';

    } else {
        alert("This browser does not support HTML5 FileReader.");
    }
});

The HTML input is:

<input type="file" id="Multifileupload" multiple="" name="file" size="40" accept=".png, .jpg, .jpeg, .gif">

Problem is when I choose some images and submit form I get empty file field in request:

csrf_mds_token=b2c47be75606853053acbbcf48c6280c&review=wreewrewrewrwrwrwrwr&rating=5&product_id=6&file=&sys_lang_id=1
2
  • Change all your var to let. See stackoverflow.com/questions/750486/… Commented May 2, 2022 at 16:32
  • I tried also name="file[]" it ends data as file[]: (binary) Commented May 2, 2022 at 17:22

1 Answer 1

1

You can not send files with the GET method, you have to use POST and add enctype="multipart/form-data" to your form tag:

<form method="POST" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

6 Comments

I will check it, because now I use this: <form action="https://modesy.store/add-review-post" method="post">
Just add enctype="multipart/form-data" to it: <form action="https://modesy.store/add-review-post" method="post" enctype="multipart/form-data">
Should name input be as name="file[]"?
When I submit form with some images I see this in body: file[]: (binary)
In which language is your backend written in? In PHP there is the $_FILES variable you can use for example
|

Your Answer

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