1

I'm trying to upload files using Dropzone JS but I got the error message:

Server responded with 400 code"

I tried setting @Html.AntiForgeryToken() in the form. I also tried sending a token:

xhr.setRequestHeader("XSRF-$('input:hidden[name="__RequestVerificationToken"]').val());

but still got the same error.

My HTML:

<form enctype="multipart/form-data" method="POST">
    @Html.AntiForgeryToken()
    <input type="text" id="Username" name="Username" />
    <div class="dropzone" id="my-dropzone" name="mainFileUploader">
        <div class="fallback">
            <input name="file" type="file" multiple />
        </div>
    </div>
</form>
<div>
    <button type="submit" id="submit-all"> upload </button>
</div>

My JS:

Dropzone.options.myDropzone = {
    url: "/Admin/Product/UploadFiles",
    autoProcessQueue: false,
    uploadMultiple: true,
    parallelUploads: 100,
    maxFiles: 100,
    //acceptedFiles: "image/*",

    init: function () {

        var submitButton = document.querySelector("#submit-all");
        var wrapperThis = this;

        submitButton.addEventListener("click", function () {
            wrapperThis.processQueue();
        });

        this.on("addedfile", function (file) {

            var removeButton = Dropzone.createElement("<button class='btn btn-lg dark'>Remove File</button>");

            removeButton.addEventListener("click", function (e) {
                // Make sure the button click doesn't submit the form:
                e.preventDefault();
                e.stopPropagation();
                wrapperThis.removeFile(file);
            });

            file.previewElement.appendChild(removeButton);
        });

        this.on('sendingmultiple', function (data, xhr, formData) {
            formData.append("Username", $("#Username").val());
            xhr.setRequestHeader("XSRF-TOKEN",
                $('input:hidden[name="__RequestVerificationToken"]').val());
        });
    }
};

My action:

[HttpPost]
public ActionResult UploadFiles()
{
    var postedUsername = Request.Form["Username"].ToString();

    // process files

    return Json(new { status = true, Message = "Account created." });
}
5
  • Your dropzone uploads files to /Admin/Product/UploadFiles, could you please show us this action method?
    – itminus
    Commented Apr 29, 2019 at 5:28
  • itminus I just updated the question.
    – user693570
    Commented Apr 29, 2019 at 17:41
  • 1
    I copied your code and it works fine for me. Is there a minimal project?
    – itminus
    Commented Apr 30, 2019 at 1:30
  • Actually I'm using this code in NopCommerce 4.1
    – user693570
    Commented Apr 30, 2019 at 17:21
  • Glad to see you've edited the thread with a NopCommerce tag :). It would be nicer if you provide more details that reproduces easily.
    – itminus
    Commented May 1, 2019 at 5:49

1 Answer 1

2

I found the solution myself, in sendingmultiple event the code should be

formData.append("__RequestVerificationToken",
                $('input:hidden[name="__RequestVerificationToken"]').val());