Dropzone 未绑定到模型

Dropzone not bound to model

我正在使用 dropzone.js 以包含其他字段的形式上传文件。

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    @Html.DropDownListFor(x => x.installation, Model.installationList, new { data_placeholder = "Select one item please" })
    @Html.ValidationMessageFor(model => model.installation, "", new { @class = "text-danger" })

<div id="files" name="files" class="dropzone"></div>

<input type="submit" value="@Resources.Global.Save" class="btn btn-default" />
}

JS:

Dropzone.options.files = {
            autoProcessQueue: false,
            uploadMultiple: true,
            parallelUploads: 100,
            maxFiles: 100,

            paramName: "files", // The name that will be used to transfer the file
            maxFilesize: 8, // MB
            url: "/ActionPlan/Home/Create"  // Same as URL generated from the form
        };

我的模型:

        // installation 
        [Display(Name = "Anomaly_Installation", ResourceType = typeof(Resources.ActionPlan))]
        public int installation { get; set; }
        public IEnumerable<SelectListItem> installationList { get; set; }

// files uploaded
        public HttpPostedFileBase[] files { get; set; }

当我提交表格时,没有文件附加到模型,但是位置数据是可以的,为什么?如何解决这个问题?

编辑:我做了一些更改,但还是有同样的问题:

HTML(剃刀)

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data", @class = "dropzone", id = "myForm" }))

我还添加了:

<div class="dropzone-previews"></div>
                <div class="fallback">
                    <!-- this is the fallback if JS isn't working -->
                    <input name="files" type="file" multiple />
                </div>

JS

Dropzone.options.files = {
            autoProcessQueue: false,
            uploadMultiple: true,
            parallelUploads: 25,
            maxFiles: 25
        };

当我检查 headers 发送时,我没有看到任何文件(这是整个表格):

------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="__RequestVerificationToken"

hQJmhZpJf0LqOo3ZZCgCUjMafbXdjNGmzM8QrnL2bjtWUerKZiyJakNJljNsM_DowRv5641qUyc0zjRcBIUh2I1AZ2LBBYko8UzrhPFvnzeWELBVBLwTmtfo6KUX5MChk_aIKvX-aEcpremYXJps1A2
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomalyId"

0
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="beginDate"

09/04/2015
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomaly"

wsqfdgsqdfsqz
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="analysis"

wsdwsdfg
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomalyTypeSelected"

2
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="piloteSelected"

52333
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomalyOriginSelected"

3
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomalyOriginData"


------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="installation"

1
------WebKitFormBoundaryAKklxx9XCCYQ22Zl--

最终解决方案: HTML:

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data", @class = "dropzone", id = "myForm" }))
{
...
<div class="fallback">
                    <!-- this is the fallback if JS isn't working -->
                    <input name="files" type="file" multiple />
                </div>
}

JS : Dropzone.autoDiscover = 假;

        var myDropzone = new Dropzone('#myForm', {
            paramName: 'files',
            autoProcessQueue: false,
            uploadMultiple: true,
            parallelUploads: 25,
            maxFiles: 25
        });

        $("form").on("submit", function (event) {
            myDropzone.processQueue(); // Tell Dropzone to process all queued files.
        });

为此,我是我的模特:

public HttpPostedFileBase[] files { get; set; }

我想您指定的选项从未被应用。这可以解释为什么在您提交表单后没有文件附加到您的模型,因为它们已经在上传时进行了处理。 要正确应用所需的选项,您需要关闭 Dropzone 中的 auto discovery function

Dropzone.autoDiscover = false;

这样你就必须 programmatically initialize Dropzone:

var myDropzone = new Dropzone('form', {
    paramName: 'files',
    autoProcessQueue: false,
    uploadMultiple: true,
    parallelUploads: 25,
    maxFiles: 1
});

Demo

autoProcessQueue

When set to false you have to call myDropzone.processQueue() yourself in order to upload the dropped files. See below for more information on handling queues.

1) 在浏览器中打开工具开发控制台,在"dropzone.uploadFile"中打断点(见图:http://www.tiikoni.com/tis/view/?id=033ad74) 2) 删除图像并检查文件是否为空

如果为空,错误可能出在后端的脚本中(通常是php、asp、ecc中的控制器)。 如果不为空,请测试干净版本的 dropzone (http://www.dropzonejs.com/) 并查看差异。

对不起我糟糕的英语:)