2012-08-17 72 views
1

我已經搜索了所有在尋找類似的問題,但不幸的是沒有人似乎很適合我。提交表格後關閉模式窗口

我在做什麼:我有一個鏈接上傳文件的主頁。當用戶點擊該鏈接它會打開一個模式窗口用一個簡單的上傳表單:

<div id="uploadResults"> 
@using (Html.BeginForm("_uploadDocument", "Request", FormMethod.Post, new { id = "uploadDoc", enctype = "multipart/form-data" })) 
{ 
    <input name="eGuid" value="@ViewBag.eGuid" id="eGuid" type="hidden" /> 
    <input name="result" id="result" value="@ViewBag.result" type="hidden" /> 
    <label for="attachment">Attachment:</label><input type="file" name="attachment" id="attachment" /> 
    <br /> 
    <input type="submit" name="submit" value="Upload" class="txtButton" /> 
} 
</div> 

在我想叫我_uploadDocument方法在控制器的形式提交。如果上傳成功,則關閉模式窗口,以便再次出現父窗口。如果出現錯誤,請保持模態窗口出現並顯示一些通知文本。

我對httppost(道歉,我試過幾種方法,所以你會看到一些註釋掉位)控制器:

[HttpPost] 
     public void _uploadDocument(string eGuid, HttpPostedFileBase attachment) 
     { 
      // store result txt 
      string result = ""; 

      // check for uploaded file 
      if (attachment != null && attachment.ContentLength > 0) 
      { 
       // get filename 
       var uploadedFileName = Path.GetFileName(attachment.FileName); 
       var newFileName = eGuid + "_" + uploadedFileName; 

       // store to the shared directory 
       var path = System.Web.Configuration.WebConfigurationManager.AppSettings["UploadDirectory"]; 
       var savePath = Path.Combine(path, newFileName); 
       try 
       { 
        attachment.SaveAs(savePath); 
        result = "success"; 
       } 
       catch 
       { 
        result = "There was an issue uploading your file"; 
       } 
      } 
      else 
      { 
       result = "No file was chosen for upload"; 
      } 

      ViewBag.result = result; 
      ViewBag.eGuid = eGuid; 
      //return PartialView("_uploadDocument"); 
      //return Json(new { result = result}); 
      //return Json(result); 
     } 

在我的模式窗口,在其上放置形式,我曾嘗試以下的jQuery:

$("#uploadDoc").submit(function (e) { 
     $.post(this.action, { eGuid: $('#eGuid').val(), attachment: $('#attachment') }, function() { 
      $("#dialog5").dialog('close'); 
      //alert("In"); 
      //return false; 
     }); 
     //$.post(this.action, { eGuid: $('#eGuid').val(), attachment: $('#attachment') }); 
     return false; 
    }); 

我的結果 - 如果我序列然後通過成功的功能火災數據,但該文件沒有上傳過(這樣的模態窗口關閉,但沒有文件被上傳)。

如果我使用的代碼如上然後將文檔上傳,但我帶到一個空白屏幕後提交...

我當然明白任何指針!

UPDATE 下面的代碼從父頁面打開模態窗口:

// person look-up modal window 
    $(function() { 
     $('#dialog5').dialog({ 
      autoOpen: false, 
      width: 400, 
      resizable: false, 
      title: 'Upload Document', 
      dataType: "json", 
      modal: true, 
      open: function (event, ui) { 
       var updateArea = $(this).data('area'); 
       //alert(updateArea); 
       $(this).load('@Url.Action("_uploadDocument", "Request")' + '?eGuid=' + updateArea); 
      }, 
      cancel: function (event, ui) { 
       $(this).dialog("close"); 
      } 
     }); 

     $('.attachDocument').live("click", function() { 
      var field = this.name; 
      $('#dialog5').data('area', field).dialog('open'); 
     }); 
    }); 
+0

我爲了這個目的是父窗口包含可能尚未保存的數據(所以我不想刷新/回發的/ etc)。也許我的方法是錯誤的 - 基本上我需要一種方式來上傳文件到服務器而不會中斷我的父表單... – 2012-08-17 16:32:51

回答

0

無法上傳通過AJAX的文件,而無需使用HTML 5,IFRAME黑客等,在我看來,那您最好的選擇可以使用上傳器,如uploadifyplupload

Binding-httppostedfilebase-using-ajax-beginform是一個類似的問題,有一些很好的建議如何解決這個問題。

+0

我已經得到了這種印象。我會檢查你的鏈接 - 但我見過其他的鏈接,讓我充滿希望,比如這個:http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax – 2012-08-17 16:27:02

+0

我會試着環顧更多。如果你發現任何東西,請告訴我。 – 2012-08-17 16:32:53

0

看起來你可能一直在正確的軌道上,但放棄了它。

您無法從void中正確地發出return語句。您是否考慮讓您的uploadDocument方法爲bool而不是void並返回成功狀態?

這種方式,你可以這樣做:

$.post(this.action, { eGuid: $('#eGuid').val(), attachment: $('#attachment') }, function (data) { 
    if(data == true) { 
    $('#uploadResults').hide(); 
    } 
}); 
+0

謝謝 - 我已經嘗試了很多不同的東西,我無法確定。我繼續前進,並將控件的更改設置爲「布爾」,並在上面的更改中設置返回值。不幸的是,這隻讓我進入一個白色的屏幕,上面寫着'True'字樣 – 2012-08-17 16:25:07

+0

然後我們錯過了他的代碼。在某個時候有一個'Respose.Redirect'發射或什麼的。 Windows不會自行更改。我們能看到更多嗎? – Wesley 2012-08-17 16:30:22

+0

真的,我唯一能想到的其他事情是打開模式窗口(從父頁面)的代碼。如果你認爲這將有助於我發佈它 – 2012-08-17 16:34:26