【问题标题】:Dropzone.js only uploads 2 files at once on each requestDropzone.js 在每个请求上一次只上传 2 个文件
【发布时间】:2019-01-22 21:54:18
【问题描述】:

我正在使用 DZ 在我的 .NET MVC Web 应用程序中上传声音文件和图像。我需要在一个请求中实现所有选定的文件一次上传。据我了解,实现这一点应该使用 DZ 属性uploadMultiple 和parallelUploads。但是,将这两个属性都设置为 true 是行不通的。

我的 HTML:

@using (Html.BeginForm("UploadSound", "ManageFiles", FormMethod.Post, new { @class = "dropzone", id = "dropzoneSound", enctype = "multipart/form-data" }))
{
    <input type="hidden" name="soundId" id="sId" value="0" />
    <div class="fallback" multiple="multiple">
        <input type="hidden" name="file" value="" />
    </div>
}

我的端点:

[HttpPost]
public ActionResult UploadSound(int soundId)
{
    var storedFilenames = new List<string>();

    if (soundId > 0)
    {
        checkFolderExist(Server.MapPath("~/sounds/" + soundId), new List<string>());

        for(int i = 0; i < Request.Files.Count; i++){
            storedFilenames.Add(SaveFile(Server.MapPath("~/sounds/" + soundId), Request.Files[i]));
        }

        return Json(new { message = storedFilenames });
    }
} 

以下代码触发了端点,但同时只上传了 2 个文件。端点被调用,直到所有文件都上传完毕。

Dropzone.options.dropzoneSound = {
        addRemoveLinks: true,
        autoProcessQueue: true,
        uploadMultiple: true,
        maxFilesize: 16, //MB
        maxFiles: 6,
        parallelUploads:6,

        removedfile: function (file) {
            var self = this;
            var s = document.getElementById('soundFile');
            var param = {
                "filename": s.value,
                "soundId": 0
            };
            $.ajax({
                type: 'POST',
                url: '@Url.Action("DeleteSoundFile", "ManageFiles")',
                data: JSON.stringify(param),
                dataType: "json",
                contentType: 'application/json',
                success: function (data) {
                    var soundFileHTML = document.getElementById('soundFile');

                    var _ref;

                    return ((_ref = file.previewElement) != null && _ref.parentNode != null) ? _ref.parentNode.removeChild(file.previewElement) : null;
                }
            });
        },

        init: function () {
            this.on("addedfile", function () {
                if (this.files[9] != null) {
                    this.removeFile(this.files[0]);
                }
            });

            //DZ only uploads 2 files at a time. when 2 files have been uploaded autoProcessQueue has to be set again to trigger next files to upload, if any
            this.on("processing", function (data) {
                this.options.autoProcessQueue = true;
            });

            this.on("complete", function (data) {

                if (data.xhr != undefined) {
                    var res = JSON.parse(data.xhr.responseText);

                    var sIds = document.getElementsByClassName('sId');

                    for (i = 0; i < sIds.length; i++) {
                        sIds[i].value = res.soundId;
                    }
                }
            });
        },
    };

当使用下面的代码修改 DZ 时,请求不会到达端点:

Dropzone.options.dropzoneSound = {
        addRemoveLinks: true,
        autoProcessQueue: true,
        uploadMultiple: true,
        maxFilesize: 16, //MB
        maxFiles: 6,
        parallelUploads:6,

有人遇到同样的问题,或者有解决问题的办法吗? 非常感谢所有帮助!

【问题讨论】:

    标签: .net asp.net-mvc dropzone.js


    【解决方案1】:

    我也遇到了这个错误,并观察到我拖入 dropzone 的文件中只有两个实际上包含在多部分表单帖子中。我的问题是我从配置对象中省略了 parallelUploads。我不确定为什么这对我有用并且对 OP 不起作用。 OP 的实现和我的一个区别是我使用带有imperatively rather than declaratively 的 Dropzone。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 2016-12-23
      • 2018-09-14
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多