我正在尝试使用ASP.NET MVC上传文件。
以下代码完美地工作:
// Read in the image data.
byte[] binaryData = null;
HttpPostedFileBase uploadedFile = Request.Files["ImageFileName"];
if (uploadedFile != null &&
uploadedFile.ContentLength > 0)
{
binaryData = new byte[uploadedFile.ContentLength];
uploadedFile.InputStream.Read(binaryData,
0,
uploadedFile.ContentLength);
}
但我试图使用在“futures”程序集中找到的新的
FileCollectionModelBinder
。我发现这两篇博客文章这里和这里解释了该怎么做。我按照这些说明操作,但是没有成功——
file
对象总是null
。以下是我的方法。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Include = "Subject, Content")]
Post post,
HttpPostedFileBase file)
{
UpdateModel(post);
...
}
请注意,我正在尝试上传文件并上传一些帖子信息到一个Post对象中。
有人可以提出任何建议吗?
为了记录,我已经在global.asax.cs中连接了ModelBinder。我也确保了表单是带有添加enctype的post:-
<form method="post" enctype="multipart/form-data" action="/post/create">