批量上传
批量的上传图片,可以更方便更加快捷。
代码
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace UploadDemo.Controllers
{
public class DefaultController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost] //表示当前动作允许Post 传递数据
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files) //files 必须前面表单里面的name的值一样
{
if (files.Count() > 0)
{
Array.ForEach(files.ToArray(), (s) =>
{
if (null != s)
{
if (!string.IsNullOrEmpty(s.FileName))
{
Response.Write(s.FileName);
}
}
});
}
return null;
}
}
}
Index页面
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/Default/Upload">
<label for=photo>Photo:</label>
<input id=files_0 type=file name=files>
<input id=files_1 type=file name=files>
<input id=files_2 type=file name=files>
<input value="Upload" type="submit">
</form>
</body>
</html>
效果