ASP.NET MVC。如何创建一个接受和多/ form-data的操作方法 [英] ASP.NET MVC. How to create Action method that accepts and multipart/form-data
问题描述
我有一个需要接受的multipart / form-data的
客户端发送一个POST请求一个控制器方法。该表单数据有2个部分吧。其一是序列化到应用程序/ JSON
,另一部分对象被作为 A照片文件的应用程序/八位字节流
。我有一个方法我的控制器是这样的:
的[AcceptVerbs(HttpVerbs.Post)
无效的ActionResult照片(PostItem后)
{
}
我可以通过 Request.File
没有问题的文件here.However的PostItem为空。
不知道为什么?任何想法
控制器code:
///<总结>
/// FeedsController
///< /总结>
公共类FeedsController:FeedsBaseController
{
的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult照片(FeedItem feedItem)
{
//这里feedItem总是空。然而Request.Files [0]给我我需要的文件
VAR处理器=新ActivityFeedsProcessor();
processor.ProcessFeed(feedItem,Request.Files [0]); SetResponse code(System.Net.HttpStatus code.OK);
返回新EmptyResult();
}
}
在电线上的客户端请求如下:
{用户代理的东西}
内容类型:多重/ form-data的;边界= 8cdb3c15d07d36a--8cdb3c15d07d36a
内容处置:表格数据; NAME =feedItem
内容类型:文本/ XML{用户ID:1234567的GroupId:123456,PostType:照片,
PublishTo:店,CreatedTime:2011-03-19 03:22:39Z}--8cdb3c15d07d36a
内容处置:文件;文件名=TESTFILE.TXT
的ContentType:应用程序/八位字节流{字节在这里。除去简洁}
--8cdb3c15d07d36a--
什么的 FeedItem
类是什么样子?对于我在后信息看它应该是这个样子:
公共类FeedItem
{
公众诠释用户ID {搞定;组; }
公众诠释的GroupId {搞定;组; }
公共字符串PublishTo {搞定;组; }
公共字符串PostType {搞定;组; }
公众的DateTime CreatedTime {搞定;组; }
}
,否则它不会被束缚。你可以尝试和更改操作的签名,看看这个作品:
[HttpPost] //AcceptVerbs(HttpVerbs.Post)是昔日的事
公众的ActionResult照片(INT用户ID,诠释的GroupId,串PublishTo
串PostType,日期CreatedTime)
{
//做一些工作在这里
}
您可以甚至尝试和 HttpPostedFileBase
参数添加到你的动作:
[HttpPost]
公众的ActionResult照片(INT用户ID,诠释的GroupId,串PublishTo
串PostType,日期CreatedTime,HttpPostedFileBase文件)
{
//最后参数消除了Request.Files需要[0]
VAR处理器=新ActivityFeedsProcessor();
processor.ProcessFeed(feedItem,文件);}
如果你真的感觉野性和调皮,添加 HttpPostedFileBase
到 FeedItem
:
公共类FeedItem
{
公众诠释用户ID {搞定;组; }
公众诠释的GroupId {搞定;组; }
公共字符串PublishTo {搞定;组; }
公共字符串PostType {搞定;组; }
公众的DateTime CreatedTime {搞定;组; }
公共HttpPostedFileBase文件{搞定;组; }
}
这最后code段可能是你想要什么直到结束,而是一步一步的细分可能会一起帮助你。
这个回答可以帮助你沿着去正确的方向,以及:<一href=\"http://stackoverflow.com/questions/3877448/asp-net-mvc-passing-model-together-with-files-back-to-controller/3878341#3878341\">ASP.NET MVC传递模型* *一起带回到控制器文件
I have a Controller method that needs to accept multipart/form-data
sent by the client as a POST request. The form data has 2 parts to it. One is an object serialized to application/json
and the other part is a photo file sent as application/octet-stream
. I have a method on my controller like this:
[AcceptVerbs(HttpVerbs.Post)]
void ActionResult Photos(PostItem post)
{
}
I can get the file via Request.File
without problem here.However the PostItem is null.
Not sure why? Any ideas
Controller Code:
/// <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();
}
}
The client request on the wire looks like this:
{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--
What does the FeedItem
class look like? For what I see in the post info it should look something like:
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; }
}
Otherwise it will not be bound. You could try and change the action signature and see if this works:
[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
}
You could even try and add a HttpPostedFileBase
parameter to your action:
[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);
}
And if you're really feeling wild and naughty, add HttpPostedFileBase
to 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; }
public HttpPostedFileBase File { get; set; }
}
This last code snippet is probably what you want to end up with, but the step-by-step breakdown might help you along.
This answer might help you along in de right direction as well: ASP.NET MVC passing Model *together* with files back to controller
这篇关于ASP.NET MVC。如何创建一个接受和多/ form-data的操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!