2011-03-18 50 views
8

我有一个Controller方法,需要接受客户端发送的multipart/form-data作为POST请求。表单数据有2个部分。一个是序列化为application/json的对象,另一个是以application/octet-stream发送的照片文件。我有这样我的控制器上的方法:ASP.NET MVC。如何创建Action方法,接受和multipart/form-data

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

我可以通过Request.File文件没有问题here.However的PostItem为空。 不知道为什么?任何想法

控制器代码:

/// <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(); 
    } 

}

电线上的客户端请求是这样的:

{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

你可以在你的视图中发布代码吗?你在使用ASP.NET MVC 1吗? – 2011-03-18 21:32:51

+0

发表了上面的代码。我正在使用MVC3 – openbytes 2011-03-19 03:26:39

回答

6

什么的FeedItem类是什么样子?对于我在帖子中看到的信息,应该看起来像这样:

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; } 
} 

否则它不会被绑定。你可以尝试和改变动作的签名,看看,如果这个工程:

[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 
} 

你甚至可以尝试和HttpPostedFileBase参数添加到您的行动:

[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); 

} 

如果你真的觉得野性顽皮,加HttpPostedFileBaseFeedItem

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; } 
} 

这最后的代码片断可能是你想结束了一下,但一步一步突破下来可能会帮助你。

这样的回答可以帮助你沿着去正确的方向,以及:ASP.NET MVC passing Model *together* with files back to controller

1

由于@Sergi说,加HttpPostedFileBase文件参数到你的动作,我不知道MVC3但对于1和2,你必须指定表格/视图,你将发布的multipart/form-data的是这样的:

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

而且这是在我的控制器:

[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); 
    } 

希望它能帮助!

相关问题