C# 在MVC ASP.NET中上载多个文件
我尝试与此只是上传一个文件,在我的情况下,我需要上传多个文件C# 在MVC ASP.NET中上载多个文件,c#,asp.net-mvc,C#,Asp.net Mvc,我尝试与此只是上传一个文件,在我的情况下,我需要上传多个文件 public ActionResult Create(HttpPostedFileBase file){ string filename = Path.GetFileName(file.FileName); string contentType = file.ContentType; string folder = Server.MapPath("~/Files"
public ActionResult Create(HttpPostedFileBase file){
string filename = Path.GetFileName(file.FileName);
string contentType = file.ContentType;
string folder = Server.MapPath("~/Files");
using (Stream fs = file.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
file.SaveAs(Path.Combine(folder, filename));
}
}
}
这是我的表格
@using (Html.BeginForm("Create","P_m",FormMethod.Post,new { @enctype = "multipart/form-data" }))
{
<input type="file" name="File" multiple />
}
@使用(Html.BeginForm(“Create”、“P_m”、FormMethod.Post、new{@enctype=“multipart/formdata”}))
{
}
我的问题是只上载一个文件,但我需要上载多个文件…您可以使用
请求。文件
或通过具有属性的视图模型:
public IEnumerable<HttpPostedFileBase> files {get;set;}
您可以使用下面的代码示例一起上传多个文件 Html代码:-
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<span>Select File:</span>
<input type="file" name="postedFiles" multiple="multiple"/>
<hr/>
<input type="submit" value="Upload"/>
<br/>
<span style="color:green">@Html.Raw(ViewBag.Message)</span>
}
</div>
</body>
</html>
指数
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
选择文件:
@Html.Raw(ViewBag.Message)
}
控制器:-
请参考我的文章显示(传递)字符串消息从控制器到ASP.NETMVC中查看
public class UploadController : Controller
{
// GET: Home
[HttpPost]
public ActionResult Index(List<HttpPostedFileBase> postedFiles)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
foreach (HttpPostedFileBase pFile in postedFiles)
{
if (postedFile != null)
{
string fileName = Path.GetFileName(pFile.FileName);
pFile.SaveAs(path + fileName);
ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
}
}
return View();
}
}
公共类上载控制器:控制器
{
//到家
[HttpPost]
公共操作结果索引(列出postedFiles)
{
字符串路径=Server.MapPath(“~/Uploads/”);
如果(!Directory.Exists(path))
{
CreateDirectory(路径);
}
foreach(postedFiles中的HttpPostedFileBase pFile)
{
if(postedFile!=null)
{
字符串文件名=Path.GetFileName(pFile.fileName);
pFile.SaveAs(路径+文件名);
ViewBag.Message+=string.Format(“{0}上传。
”,文件名);
}
}
返回视图();
}
}
添加更多
并使用请求.Files
在控制器的操作中获取HttpPostedFileBase
的可枚举项。@jegtugado我需要使用一个上载文件,然后跳过添加更多输入,并在操作中使用请求.Files
。
public class UploadController : Controller
{
// GET: Home
[HttpPost]
public ActionResult Index(List<HttpPostedFileBase> postedFiles)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
foreach (HttpPostedFileBase pFile in postedFiles)
{
if (postedFile != null)
{
string fileName = Path.GetFileName(pFile.FileName);
pFile.SaveAs(path + fileName);
ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
}
}
return View();
}
}