c# - Dropzone.js 已附加

标签 c# jquery asp.net-mvc asp.net-mvc-5 dropzone.js

我已经在我的项目中实现了 dropzone,一切都运行良好。除了浏览器决定在请求页面清晰时抛出错误我已经将 View 修剪得很长,但下面的 HTML 当前位于表单内(不确定这是否会导致问题)我只有这里显示了对 dropzone 的一个引用,dropzone.js 再次包含在捆绑配置中,仅出现一次。

错误:if(this.element.dropzone)抛出新错误(“Dropzone已附加。”)

这是我的观点

<div class="form-group">
    <div class="col-xs-12 dropzone" id="UploadImage">
         <input type="file" id="id-input-file-2" />
     </div>
</div>

这就是我创建拖放区的方式

 $(document).ready(function () {

        $("div#UploadImage").dropzone({ url: '@Url.Action("SaveUploadedFile", "Person")' });
 });

我的 Controller 如下

 public ActionResult SaveUploadedFile()
    {
        bool success = true;

        string fName = string.Empty;

        try
        {
            foreach (var file in Request.Files.Cast<string>().Select(fileName => Request.Files[fileName]).Where(file => file != null))
            {
                fName = file.FileName;

                if (file.ContentLength > 0)
                {
                   // will write the rest of the code here
                }
            }
        }
        catch (Exception ex)
        {
            success = false;
        }

        return Json(success ? new { Message = fName } : new { Message = "Error in saving file" });
    }

现在我可以在 Controller 内检索图像,只是不确定代码中的放置区再次初始化的位置,这导致了上述错误。

更新

尝试戴夫的建议后,我的 jquery 现在看起来像这样

$(document).ready(function () {

        Dropzone.options.myAwesomeDropzone = false;

        $("div#UploadImage").dropzone({
            url: '@Url.Action("SaveUploadedFile", "Person")',
            addRemoveLinks: true,
            removedfile: function (file) {
                var name = file.name;
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("DeleteUploadedFile", "Person")',
                    data: "id=" + name,
                    dataType: 'html'
                });
                var ref;
                return (ref = file.previewElement) != null ? ref.parentNode.removeChild(file.previewElement) : void 0;
            },
            maxFilesize: 2,
            maxFiles: 12,

        });
});

但是我仍然收到错误。

最佳答案

尝试从 Reference here

  1. 像这样全局关闭自动发现:Dropzone.autoDiscover = 假;或者

  2. 关闭特定元素的自动发现,如下所示: Dropzone.options.myAwesomeDropzone = false;

关于c# - Dropzone.js 已附加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27324811/

相关文章:

c# - 在 .NET Microframework 中找到三个 double 中最大的最有效方法?

javascript - 如何使用 Jquery 为按钮设置样式

jquery - 准确的高度数字

c# - 是什么导致 Ghostscript 返回 -100 错误?

asp.net - Html.Partial() 跳过 Controller Action

当值太大时 C# 代码返回方法外

c# - 如何在 Windows Azure 中部署 WCF

c# - 为什么我得到编译器错误 CS0120 : An object reference is required for the non-static field, 方法或属性

javascript - div 在 <option> 标签或替代?

asp.net - 无法在 SessionStorage 中设置 null