2011-05-09 62 views
1

我的對話框在成功上傳後不會彈出?上傳工作正常,但$(「#dialog-confirm」)。對話框不起作用?上傳asp.net mvc後不會彈出對話框2

<h2> 
    upload Data</h2> 
<script type="text/javascript" src="http://www.google.com/jsapi"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> 
<script src="../../Scripts/jqueryform.js" type="text/javascript"></script> 
<script src="../../Scripts/jblock.js" type="text/javascript"></script> 

<script src="../../Content/jquery-ui-1.8.12.custom/js/jquery-ui-1.8.12.custom.min.js" 
    type="text/javascript"></script> 

<link href="../../Content/jquery-ui-1.8.12.custom/css/ui-lightness/jquery-ui-1.8.12.custom.css" 
    rel="stylesheet" type="text/css" /> 



<script type="text/javascript"> 
    $(function() { 
     $("#ajaxUploadForm").ajaxForm({ 
      iframe: true, 
      dataType: "json", 
      beforeSubmit: function() { 
       $("#ajaxUploadForm").block({ message: '<h1><img src="/Content/busy.gif" /> uploading file...</h1>' }); 
      }, 
      success: function (result) { 
       $("#ajaxUploadForm").unblock(); 
       $("#ajaxUploadForm").resetForm(); 
       $.growlUI(null, result.message); 

       //alert(result.message); 

       //does not popup?? 
       $("#dialog-confirm").dialog({ 
        resizable: false, 
        height: 140, 
        modal: true, 
        buttons: { 
         "Ok": 
         function() { 
          alert('ok'); 
          $(this).dialog("close"); 
         } 
        , 
         Cancel: function() { 
          $(this).dialog("close"); 
         } 
        } 
       }); 


      }, 
      error: function (xhr, textStatus, errorThrown) { 
       $("#ajaxUploadForm").unblock(); 
       $("#ajaxUploadForm").resetForm(); 
       $.growlUI(null, 'Error uploading file'); 
      } 
     }); 
    }); 
</script> 
<form id="ajaxUploadForm" action="<%= Url.Action("AjaxUpload", "Home")%>" method="post" enctype="multipart/form-data"> 
<fieldset> 
    <legend>Upload a file</legend> 
    <label> 
     File to upload: 
     <input type="file" name="file" />(100MB max size)</label> 
    <input id="ajaxUploadButton" type="submit" value="Submit" /> 
</fieldset> 
</form> 


    public FileUploadJsonResult AjaxUpload(HttpPostedFileBase file) 
    { 
     // TODO: Add your business logic here and/or save the file 
     System.Threading.Thread.Sleep(2000); // Simulate a long running upload 

     // Return JSON 
     return new FileUploadJsonResult { Data = new { message = string.Format("{0} uploaded successfully.", System.IO.Path.GetFileName(file.FileName)) } }; 
    } 

回答

0

我看到你正在使用的jquery.form插件AJAXifying形式,並從控制器動作返回JSON。下面介紹一下documentation說,關於這樣的場景:

由於無法上傳使用瀏覽器的 XMLHttpRequest對象 文件,這個插件 使用一個隱藏的iframe元素,以幫助 與任務。這是一種常用的技術,但它具有固有的 限制。 iframe元素爲 ,用作表單的 提交操作的目標,這意味着 服務器響應被寫入到iframe的 。如果響應 類型爲HTML或XML,但如果響應類型爲腳本 或JSON,兩者通常都包含 字符需要轉用 在發現時使用實體引用,則無效 HTML標記。

要佔 腳本和JSON響應的挑戰,形式 插件允許這些反應是 嵌入到textarea元素,它 建議您在 一起選擇使用時 這樣做這些響應類型與文件上傳。

所以這個工作從服務器的響應需要看起來像這樣:

<textarea>{ message: 'file uploaded successfully' }</textarea> 

難道這是什麼風俗FileUploadJsonResult在你的控制器動作是幹什麼的?

+0

沒錯,這就是我的FileUploadJsonResult:公共類FileUploadJsonResult:JsonResult { 公共覆蓋無效的ExecuteReuslt(ControllerContext上下文) { this.ContentType = 「text/html的」; context.HttpContext.Response.Write(「」); } } – user603007 2011-05-09 07:52:10