Pur*_*ome 5 asp.net-mvc file-upload
我正在尝试使用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);
}
Run Code Online (Sandbox Code Playgroud)
但我想要做的是使用期货装配中的新FileCollectionModelBinder发现.
我在这里和这里发现了这两篇博客文章,解释了该怎么做.我按照这些说明但没有运气 - > file对象总是如此null.
这是我的方法.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Include = "Subject, Content")]
Post post,
HttpPostedFileBase file)
{
UpdateModel(post);
...
}
Run Code Online (Sandbox Code Playgroud)
请注意我是如何上传文件并将一些帖子信息上传到Post对象的.
有人可以提出任何建议吗?
为了记录,我在我的global.asax.cs中连接了ModelBinder.我还确保该表单是添加了enctype的帖子: -
<form method="post" enctype="multipart/form-data" action="/post/create">
Run Code Online (Sandbox Code Playgroud)
Pur*_*ome 10
没有血腥的方式:(
我想出了答案,这很蹩脚.
我必须让参数NAME与input type ="file"元素的Id/Name值相同!(不确定它是否是元素值中的任何一个或两个......我没有检查出来).
所以这就是答案.
<input type="file" id="imageFileName" name="imageFileName" class="upload" />
Run Code Online (Sandbox Code Playgroud)
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Include = "Subject, Content")]Post post,
HttpPostedFileBase imageFileName)
{
...
}
Run Code Online (Sandbox Code Playgroud)
shees!
| 归档时间: |
|
| 查看次数: |
8828 次 |
| 最近记录: |