2013-05-31 44 views
2

我正在處理asp.net mvc 3應用程序。標題說明我有什麼問題。我將描述我如何得到這個錯誤。Microsoft JScript運行時錯誤:無法獲取屬性'removeChild'的值:對象爲空或未定義

我使用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的非常基本的知識。我使用這個代碼,但我不明白它的一些部分。在這裏 - 通過評論和代碼本身有點明顯是怎麼回事,但如果我評論這一點,並嘗試上傳圖片,我不會收到錯誤,這是非常誘人的事情,但我認爲最可能的代碼是好的,得到這個錯誤的原因是在別的地方,所以我在這裏發佈以獲得任何可能導致此錯誤的幫助,以及如何解決這個錯誤?

PS

這是我使用的上傳圖片的形式:

@using (Html.BeginForm("Upload", "Forms", FormMethod.Post)) 
{ 
    <input [email protected][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> 
} 

回答

0

這是setTimeout的

語法
setTimeout(function(),milliseconds); 

所以,你應該做這樣的事情

setTimeout(function(){ 
      iframeId.parentNode.removeChild(iframeId); 
      }, 250); 
+0

嗯,我得到了e像前面的代碼一樣xact相同的錯誤,但至少現在我可以在VS2010中進行某種調試,它顯示'parentNode'實際上是Null。我不確定這是什麼意思,在整個函數的上下文中,但我認爲只要執行某種類型的檢查,如「if(iframeId!= null){//這裏剩下的碼; }'或者必須有父節點,並且某個地方出了問題,如果它爲空? – Leron

+0

你的iframeId在哪裏......?有沒有父母......? –

+0

嗯,我認爲父母在這裏設置 - 'form.parentNode.appendChild(iframe);'。並查看錶單看起來像檢查我編輯的問題,因爲我不能在這裏發佈。 – Leron

相關問題