Why is IFormFile collection empty when sent from Dropzone.js?

泪湿孤枕 提交于 2019-12-08 20:47:29

Check that your dropzone settings are getting applied correctly. I have tried your code as-is and it worked fine for me. However, if I removed the Dropzone configuration from the page then I get a filecount of 0.

To get around this problem put the dropzone configuration into the .cshtml page that contains the dropzone and you should see it working OK for example:

Index.cshtml

<div>
    <form action="/api/images/upload"
          class="dropzone needsclick dz-clickable"
          id="image-upload"
          method="post"
          enctype="multipart/form-data">
        <div class="dz-message needsclick">
            <span class="note needsclick">
                Drop files here or click to upload.
            </span>
        </div>
    </form>
</div>

@section Scripts {
<script>
    /* Dropzone */
    // "imageUpload" is the camelized version of the HTML element's ID
    Dropzone.options.imageUpload = {
        paramName: "files", // The name that will be used to transfer the file
        dictDefaultMessage: "Drop files here or Click to Upload",
        addRemoveLinks: true, // Allows for cancellation of file upload and remove thumbnail            
        init: function () {
            myDropzone = this;
            myDropzone.on("success", function (file, response) {
                console.log("Success");
                myDropzone.removeFile(file);
            });
        }
    };
</script>
}

Now, if you delete the @section from the page you will start getting a files.count of 0 when you try to upload the files.

If you want to have the dropzone configuration in a separate file then you need to ensure it is loaded into the page correctly e.g. change your scripts section to:

@section scripts {
    <script src="~/scripts/dropzone-config.js"></script>
}

...using the correct path to your dropzone configuration file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!