2011-08-23 48 views
0

我試圖重新創建MVC3中的WordPress圖像上傳功能,其中彈出允許您插入圖像url和編輯圖像屬性,如alt標籤,大小和位置在它被上傳到服務器之後。我嘗試過使用jqueryUi和一個上傳的ActionResult,並且能夠上傳文件,但是現在我試圖將上傳的URL返回到彈出窗口。幫助重新創建Wordpress圖像上傳與ASP MVC3

任何人都試過併成功與此?

編輯:

這裏是我的ActionResult是上傳並準備上傳的圖片的URL。

[HttpPost] 
public ActionResult Upload(HttpPostedFileBase file) 
{ 
    if (file != null && file.ContentLength > 0) 
    { 
    var fileName = Path.GetFileName(file.FileName); 
    var path = Path.Combine(Server.MapPath("~/Content/Uploads"), fileName); 
    file.SaveAs(path); 
    //Below is the path I want returned to mypop-up 
    ViewBag.WebPath = "/Content/Uploads/" + fileName; 
    } 
    //no idea which Return string to use... 
} 

我jQueryUI的對話框:

$('#upload-dialog').dialog({ 
      autoOpen: false, 
      width: 600, 
      resizable: false, 
      title: 'Upload Image', 
      modal: true, 
      open: function (event, ui) { 
       $(this).load('@Url.Action("UploadImagePartial")'); 
      }, 
      buttons: { 
       "Close": function() { 
        $(this).dialog("close"); 
       } 
      } 
     }); 
+0

任何使用有想法json或ajax? – Ron

回答

1

一個想法是使用Ajax張貼到JsonResult動作與含上傳URL的對象響應。

$.ajax({ 
    url: "/Controller/Action", 
    type: "POST", 
    data: { imgData: imgData }, 
    success: function(receive) { 
     // Do whatever 
    } 
}) 

當你的行動會看起來像

public JsonResult Action(string imgData) 
{ 
    // Do whatever upload logic you have 
    var imagePath = /* your path */ 

    return Json(new { path = imagePath }); 
} 

然後在你的Ajax成功功能,您將能夠訪問路徑,例如:

var jsPath = receive.path

+0

我在哪裏放第一個代碼塊?對不起,沒有exp。與json呢。我想要做的是在我上傳文件後,我想讓網址返回到模式彈出窗口進行驗證。但我不知道將什麼返回值放到我的ActionResult中,以使我回到模態彈出窗口。將上面發佈我的ActionResult。 – Ron

+0

如何將此鏈接與我上面的jqeryui對話框連接起來? – Ron

+0

對不起,隊友,從來沒有使用莫代爾彈出窗口,不能幫助你在這裏 –