2011-03-18 25 views
8

Eylem yöntemi oluşturma İstemci tarafından POST isteği olarak gönderilen multipart/form-data kabul etmesi gereken bir Denetleyici yöntem var. Form verisinin 2 bölümü vardır. Biri, application/json için serileştirilmiş bir nesnedir ve diğer kısım ise application/octet-stream olarak gönderilen bir fotoğraf dosyasıdır.ASP.NET MVC. Nasıl kabul edilir ve çok parçalı/form veri

[AcceptVerbs(HttpVerbs.Post)] 
void ActionResult Photos(PostItem post) 
{ 
} 

Ben PostItem null here.However sorunsuz Request.File aracılığıyla dosyayı alabilirsiniz: Bu gibi benim denetleyicisinde bir yöntem var. Neden olmadığından emin değil misiniz? Herhangi bir fikir

Kontrolör Kodu:

/// <summary> 
/// FeedsController 
/// </summary> 
public class FeedsController : FeedsBaseController 
{ 
    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Photos(FeedItem feedItem) 
    { 
     //Here the feedItem is always null. However Request.Files[0] gives me the file I need 
     var processor = new ActivityFeedsProcessor(); 
     processor.ProcessFeed(feedItem, Request.Files[0]); 

     SetResponseCode(System.Net.HttpStatusCode.OK); 
     return new EmptyResult(); 
    } 

}

telin üzerinde istemci isteği şuna benzer:

{User Agent stuff} 
Content-Type: multipart/form-data; boundary=8cdb3c15d07d36a 

--8cdb3c15d07d36a 
Content-Disposition: form-data; name="feedItem" 
Content-Type: text/xml 

{"UserId":1234567,"GroupId":123456,"PostType":"photos", 
    "PublishTo":"store","CreatedTime":"2011-03-19 03:22:39Z"} 

--8cdb3c15d07d36a 
Content-Disposition: file; filename="testFile.txt" 
ContentType: application/octet-stream 

{bytes here. Removed for brevity} 
--8cdb3c15d07d36a-- 
+2

Görünümünüze kod gönderebilir misiniz? ASP.NET MVC 1 kullanıyor musunuz? –

+0

Yukarıdaki kodu gözden geçirin. MVC3 kullanıyorum – openbytes

cevap

6

neye benzer FeedItem sınıf görünüyor? Gönderi bilgisinde gördüğüm şey için şu gibi bir şey görünmelidir:

public class FeedItem 
{ 
    public int UserId { get; set; } 
    public int GroupId { get; set; } 
    public string PublishTo { get; set; } 
    public string PostType { get; set; } 
    public DateTime CreatedTime { get; set; } 
} 

Aksi taktirde bağlı olmayacaktır. Denemek ve eylem imzasını değiştirip bakın bu çalışırsa olabilir:

[HttpPost] //AcceptVerbs(HttpVerbs.Post) is a thing of "the olden days" 
public ActionResult Photos(int UserId, int GroupId, string PublishTo 
    string PostType, DateTime CreatedTime) 
{ 
    // do some work here 
} 

denemek bile olabilir ve harekete geçirme HttpPostedFileBase parametreyi ekleyin:

[HttpPost] 
public ActionResult Photos(int UserId, int GroupId, string PublishTo 
    string PostType, DateTime CreatedTime, HttpPostedFileBase file) 
{ 
    // the last param eliminates the need for Request.Files[0] 
    var processor = new ActivityFeedsProcessor(); 
    processor.ProcessFeed(feedItem, file); 

} 

Ve gerçekten vahşi hissediyorsanız ve

public class FeedItem 
{ 
    public int UserId { get; set; } 
    public int GroupId { get; set; } 
    public string PublishTo { get; set; } 
    public string PostType { get; set; } 
    public DateTime CreatedTime { get; set; } 
    public HttpPostedFileBase File { get; set; } 
} 

Bu son kod parçası ile bitirmek istediklerini muhtemelen, ama adım adım molası: yaramaz, HttpPostedFileBaseFeedItem eklemek Aşağı size yardımcı olabilir. ASP.NET MVC passing Model *together* with files back to controller

1

@Sergi demek gibi, eylem HttpPostedFileBase dosya parametre eklemek ve ben MVC3 için bilmiyorum ama 1 ve 2 için için gerekenler:

Bu cevap aynı zamanda de doğru yönde size yardımcı olabilir

<% using (Html.BeginForm(MVC.Investigation.Step1(), FormMethod.Post, new { enctype = "multipart/form-data", id = "step1form" })) 

Ve bu benim denetleyicisi olduğu: böyle çok parçalı/formu-veri göndermek edeceği biçim/görünümünde belirtmek

[HttpPost] 
    [ValidateAntiForgeryToken] 
    [Authorize(Roles = "Admin, Member, Delegate")] 
    public virtual ActionResult Step1(InvestigationStep1Model model, HttpPostedFileBase renterAuthorisationFile) 
    { 
     if (_requesterUser == null) return RedirectToAction(MVC.Session.Logout()); 

     if (renterAuthorisationFile != null) 
     { 
      var maxLength = int.Parse(_configHelper.GetValue("maxRenterAuthorisationFileSize")); 
      if (renterAuthorisationFile.ContentLength == 0) 
      { 
       ModelState.AddModelError("RenterAuthorisationFile", Resources.AttachAuthorizationInvalid); 
      } 
      else if (renterAuthorisationFile.ContentLength > maxLength * 1024 * 1204) 
      { 
       ModelState.AddModelError("RenterAuthorisationFile", string.Format(Resources.AttachAuthorizationTooBig, maxLength)); 
      } 
     } 
     if(ModelState.IsValid) 
     { 
      if (renterAuthorisationFile != null && renterAuthorisationFile.ContentLength > 0) 
      { 
       var folder = _configHelper.GetValue("AuthorizationPath"); 
       var path = Server.MapPath("~/" + folder); 
       model.RenterAuthorisationFile = renterAuthorisationFile.FileName; 
       renterAuthorisationFile.SaveAs(Path.Combine(path, renterAuthorisationFile.FileName)); 
      } 
      ... 
      return RedirectToAction(MVC.Investigation.Step2()); 
     } 
     return View(model); 
    } 

yardımcı olur Umut!