I'm working on an asp.net mvc 3 application. The title tells what problem I have. I will describe how I get this error.
I'm using JavaScript to upload image from my razor view. The script is not that long so I'll post it here :
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');
}
By using this script I get the image in my controller where I do my business logic so I'm not sure if it's really the script that is causing this, but I can't find another reason while debugging. So when the controller finish its work I get a new tab open in the visual studio 2010 titled Script block[dynamic]
and the following code is there :
function anonymous()
{
iframeId.parentNode.removeChild(iframeId)
}
which is actually part of my JavaScript :
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
I have very basic knowledge about JS. I use this code but I don't understand some parts of it. Here - by the comment and the code itself is somewhat obvious what's going on but if I comment this and try to upload an image I don't get error and this is something very tempting to do, but I think that most probably the code is OK and the reason to get this error is somewhere else, so I post here to get any help on what may cause this error and how can it be fixed?
P.S
And this is the form that I use for uploading the image:
@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>
}