我正在使用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(Razor)
@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
};
当我检查发送的标题时,我没有看到任何文件(这是整个表单):
------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 = false;
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; }
答案 0 :(得分:5)
我猜你指定的选项永远不会被应用。这可以解释为什么一旦提交表单就没有文件附加到您的模型,因为它们已经在上传时处理过。 要正确应用所需的选项,您需要关闭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
});
设置为false时,您必须自己调用myDropzone.processQueue()才能上传删除的文件。有关处理队列的更多信息,请参见下文。
答案 1 :(得分:1)
1)在浏览器中打开工具开发控制台,并在&#34; dropzone.uploadFile&#34;中放置一个断点。 (见图片:http://www.tiikoni.com/tis/view/?id=033ad74) 2)删除图像,检查文件是否为空
如果为空,则错误可能在后端脚本中(通常是php,asp,ecc中的控制器)。 如果不为空,请测试一个干净的dropzone版本(http://www.dropzonejs.com/)并查看差异。
对不起我可怕的英语:)