asp.net-mvc – ASP.NET MVC.如何创建接受和multipart / form-data的Actio
发布时间:2020-05-24 10:58:31 所属栏目:asp.Net 来源:互联网
导读:我有一个Controller方法,需要接受客户端发送的多部分/表单数据作为POST请求.表单数据有2个部分.一个是序列化为application / json的对象,另一个是作为application / octet-stream发送的照片文件.我的控制器上有一个方法,如下所示: [AcceptVerbs(HttpVerbs.Po
|
我有一个Controller方法,需要接受客户端发送的多部分/表单数据作为POST请求.表单数据有2个部分.一个是序列化为application / json的对象,另一个是作为application / octet-stream发送的照片文件.我的控制器上有一个方法,如下所示: [AcceptVerbs(HttpVerbs.Post)]
void ActionResult Photos(PostItem post)
{
}
我可以在这里通过Request.File获取文件没有问题.但是PostItem为null. 控制器代码: /// <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--
解决方法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,DateTime CreatedTime,HttpPostedFileBase file)
{
// the last param eliminates the need for Request.Files[0]
var processor = new ActivityFeedsProcessor();
processor.ProcessFeed(feedItem,file);
}
如果你真的感觉疯狂和顽皮,请将HttpPostedFileBase添加到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; }
}
最后一段代码片段可能就是您想要的结果,但逐步细分可能对您有所帮助. 这个答案也可以帮助你朝着正确的方向前进:ASP.NET MVC passing Model *together* with files back to controller (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-core – ASP.NET核心DisplayAttribute本地化
- asp.net – 如何更改F#Canopy UI测试脚本中的下拉列表
- ASP.NET Ajax回发突然停止在IPhone / IPad上
- asp.net-mvc – 使用Web Deploy发布ASP.NET MVC2站点
- asp.net – 脚本标签和链接标签进入asp:内容或外部
- asp.net – sql server报告服务和其他报告工具之间有什么区
- asp.net会员提供者Guid userID
- asp.net – 在没有完全回发的情况下,在AJAX更新面板中上传文
- Asp.net Webform显示警报和重定向
- asp.net-mvc-3 – _AppStart正在执行时无法创建存储范围
推荐文章
站长推荐
- asp.net-mvc – 支持URL中任何位置的catch-all参
- asp.net – bing地图花钱?
- asp.net-mvc – ASP.NET MVC:除了IE之外,我如何
- asp.net jQuery Ajax用户登录功能的实现
- asp.net – 从Application_BeginRequest()中设置
- asp.net – 为什么HttpContext.Current.User.Ide
- ASP.NET Web.config AppSettings性能
- asp.net-mvc-3 – 如何增加会话超时MVC 3
- 使用ASP.NET Web API 2.1配置依赖注入
- asp.net-mvc – MVC3 EF4 POCO存储库/ UnitOfWor
热点阅读
