我已经在我的项目中实现了 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
像这样全局关闭自动发现:Dropzone.autoDiscover = 假;或者
关闭特定元素的自动发现,如下所示: Dropzone.options.myAwesomeDropzone = false;
关于c# - Dropzone.js 已附加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27324811/