【发布时间】:2013-05-31 06:09:52
【问题描述】:
我正在开发一个 asp.net mvc 3 应用程序。标题告诉我有什么问题。我将描述我是如何得到这个错误的。
我正在使用 JavaScript 从我的剃刀视图上传图像。脚本不是很长,所以我会在这里发布:
function fileUpload(form, action_url, div_id) {
// Create the iframe...
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "upload_iframe");
iframe.setAttribute("name", "upload_iframe");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");
// Add to document...
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name = "upload_iframe";
iframeId = document.getElementById("upload_iframe");
// Add event...
var eventHandler = function () {
if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);
// Message from server...
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}
document.getElementById(div_id).innerHTML = content;
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
}
if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);
// Set properties of form...
form.setAttribute("target", "upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("encoding", "multipart/form-data");
// Submit the form...
form.submit();
document.getElementById(div_id).innerHTML = "Uploading...";
form.setAttribute("action", '/forms/displayform');
}
通过使用此脚本,我在执行业务逻辑的控制器中获取了图像,因此我不确定是否真的是导致此问题的脚本,但在调试时我找不到其他原因。因此,当控制器完成其工作时,我会在 Visual Studio 2010 中打开一个名为 Script block[dynamic] 的新选项卡,并提供以下代码:
function anonymous()
{
iframeId.parentNode.removeChild(iframeId)
}
这实际上是我的 JavaScript 的一部分:
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
我对 JS 有非常基本的了解。我使用此代码,但我不理解其中的某些部分。在这里-通过评论和代码本身有点明显发生了什么,但是如果我对此发表评论并尝试上传图片,我不会收到错误,这是非常诱人的事情,但我认为代码很可能是好的,出现此错误的原因是在其他地方,所以我在这里发布以获取有关可能导致此错误的原因以及如何修复它的任何帮助?
附言
这是我用来上传图片的表格:
@using (Html.BeginForm("Upload", "Forms", FormMethod.Post))
{
<input name=@Model[0].DocumentId type="hidden" />
<input type="file" name="datafile" id="file" onchange="readURL(this);" />
<input type="button" name="Button" value="Upload Image" id="UploadButton" onclick="fileUpload(this.form,'/forms/upload','uploadImg'); return false;"/>
<div id="uploadImg" style="display: inline-block;">
<img id="blah" src="#" alt="your image" style="display:none;"/>
</div>
}
【问题讨论】:
标签: c# javascript asp.net-mvc-3 razor