用Html5与Asp.net MVC上传多个文件的实现代码
更新时间:2012年08月30日 11:57:35 作者:
Html 5 的有一些File API,对Form表单增强的特性,让我们轻松支持多文件上传,看下面的Html片断代码
复制代码 代码如下:
<form action="/Home/Upload" enctype="multipart/form-data" id="form2" method="post">
<input type="file" name="fileToUpload" id="fileToUpload2" multiple="multiple" />
<input type="submit" value="submit" />
</form>
那在Asp.net MVC web application中,我们可以这么实现:
复制代码 代码如下:
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "form2" }))
{
<label for="file">Upload Image:</label>
<input type="file" name="fileToUpload" id="fileToUpload2" multiple="multiple" />
<input type="submit" value="Upload Image by submit" />
}
假设这是一个HomeController下View, 即将提交到Upload的Action,看下面服务端的代码:
复制代码 代码如下:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase[] fileToUpload)
{
foreach (HttpPostedFileBase file in fileToUpload)
{
string path = System.IO.Path.Combine(Server.MapPath("~/App_Data"), System.IO.Path.GetFileName(file.FileName));
file.SaveAs(path);
}
ViewBag.Message = "File(s) uploaded successfully";
return RedirectToAction("Index");
}
好的,就这么简单。 这里我们把接收到文件存储到App_Data文件夹中,然后返回Index的Action. 看下面图片,我们能够从文件选择器选择多张图片:
关于HTML5这个特性在那些浏览器支持,您可以去这里查看。 您还可以查看W3C官方的文档。我们在FireFox 14.01下测试能过。
希望对您Web开发有帮助。
相关文章
asp.net动态产生checkbox(数据源为DB或内存集合)
动态产生一组checkbox(数据源为DB或内存集合)且post提交时后台能及时获取等等,打算使用repeater+input(checkbox)+input(hidden)来实现2013-10-10ASP.NET获取MS SQL Server安装实例实现思路及代码
在演示中,是把找到的实例显示于DropDownList控件中。首先在.aspx拉一个DropDownList控件,感兴趣的朋友可以了解下哦,或许对你有所帮助2013-01-01TreeNodeCheckChanged事件触发方法代码实例
这篇文章主要介绍了TreeNodeCheckChanged事件触发方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-12-12
最新评论