【发布时间】:2014-12-05 21:16:54
【问题描述】:
我已经在我的项目中实施了 dropzone,一切都很好。除了浏览器决定在页面请求清晰时抛出错误之外,我已将视图修剪为很长,但下面的 HTML 当前位于表单中(不确定这是否会导致问题)我只有对此处显示的 dropzone 的一次引用,dropzone.js 再次包含在捆绑配置中,仅出现一次。
错误 : if(this.element.dropzone)throw new Error("Dropzone already attach.")
这是我的看法
<div class="form-group">
<div class="col-xs-12 dropzone" id="UploadImage">
<input type="file" id="id-input-file-2" />
</div>
</div>
这就是我创建dropzone的方式
$(document).ready(function () {
$("div#UploadImage").dropzone({ url: '@Url.Action("SaveUploadedFile", "Person")' });
});
和我的控制器如下
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" });
}
现在我能够在控制器中检索图像,只是不确定 dropzone 在代码中的哪个位置再次被初始化,这导致了上述错误。
更新
在尝试了 Daves 的建议后,我的 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,
});
});
但我仍然得到错误。
【问题讨论】:
标签: c# jquery asp.net-mvc asp.net-mvc-5 dropzone.js