比如,我们要在一个MVC页面中,实现多个地方图片上传。那么就会存在一个页面中有多个提交按钮的问题。在网上也搜了很多答案,都不太好使,最后还是自己研究出来了,把解决方法贴出来,希望能给大家一些帮助。
aspx页面的代码如下:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server" >
<h2 >
网站Logo管理</h2 >
<table >
<tr >
<td >
说明:只能上传gif图片,网站不限定图片大小,请上传合适大小的图片,如果图片大小过大,会造成网站变形等问题。
</td >
</tr >
<tr >
<td >
Logo:<input name="uploadpic" type="file" size="80" / >
<input type="submit" value="保存" name="action" / >
<div class="red" >
<%:Html.ValidationMessage("validationLogoError")% ></div >
</td >
</tr >
</table >
<br / >
<br / >
<h2 >
网站幻灯片管理</h2 >
<table >
<tr >
<td >
说明:只能上传gif图片,推荐大小为1000pxX350px,如果原图不是该大小,网站会自动将图片拉伸为该大小,可能会造成图片变形等问题。
</td >
</tr >
<tr >
<td >
幻灯片一:<input name="uploadpic1" type="file" size="80" / >
<input type="submit" value="保存1" name="action" / >
<div class="red" >
<%:Html.ValidationMessage("validationLogoError1")% ></div >
</td >
</tr >
<tr >
<td >
幻灯片二:<input name="uploadpic2" type="file" size="80" / >
<input type="submit" value="保存2" name="action" / >
<div class="red" >
<%:Html.ValidationMessage("validationLogoError2")% ></div >
</td >
</tr >
<tr >
<td >
幻灯片三:<input name="uploadpic3" type="file" size="80" / >
<input type="submit" value="保存3" name="action" / >
<div class="red" >
<%:Html.ValidationMessage("validationLogoError3")% ></div >
</td >
</tr >
<tr >
<td >
幻灯片四:<input name="uploadpic4" type="file" size="80" / >
<input type="submit" value="保存4" name="action" / >
<div class="red" >
<%:Html.ValidationMessage("validationLogoError4")% ></div >
</td >
</tr >
</table >
</asp:Content >
大家可以看到,我这个页面需要上传一个logo和四个幻灯片的图片,一共有五个提交按钮。注意,我这里每个提交按钮的value是不一样的,而name都是"action",这是必须的,因为我们后台就靠这个来name来获取到value的值,再根据value值来区别不同的提交按钮。
action中的处理方法:
[HttpPost]
public ActionResult Logo(string Action)
{
switch (Action)
{
case "保存":
{
if (Request.Files.Count != 0)
{
HttpPostedFileBase file = Request.Files["uploadpic"];
string Extension = Path.GetExtension(file.FileName);
string newFileName = "logo" + Extension;
if (!Extension.Equals(".gif"))
{
ModelState.AddModelError("validationLogoError", "只能上传gif类型图片");
}
else
{
file.SaveAs(Path.Combine(Server.MapPath("/Content/Logo/"), newFileName));
return Content("<script >alert('保存成功!');location.href='logo';</script >");
}
}
break;
}
case "保存1":
{
if (Request.Files.Count != 0)
{
HttpPostedFileBase file = Request.Files["uploadpic1"];
string Extension = Path.GetExtension(file.FileName);
string newFileName = "1" + Extension;
if (!Extension.Equals(".gif"))
{
ModelState.AddModelError("validationLogoError1", "只能上传gif类型图片");
}
else
{
file.SaveAs(Path.Combine(Server.MapPath("/Content/Logo/"), newFileName));
return Content("<script >alert('保存成功!');location.href='logo';</script >");
}
}
break;
}
case "保存2":
{
if (Request.Files.Count != 0)
{
HttpPostedFileBase file = Request.Files["uploadpic2"];
string Extension = Path.GetExtension(file.FileName);
string newFileName = "2" + Extension;
if (!Extension.Equals(".gif"))
{
ModelState.AddModelError("validationLogoError2", "只能上传gif类型图片");
}
else
{
file.SaveAs(Path.Combine(Server.MapPath("/Content/Logo/"), newFileName));
return Content("<script >alert('保存成功!');location.href='logo';</script >");
}
}
break;
}
case "保存3":
{
if (Request.Files.Count != 0)
{
HttpPostedFileBase file = Request.Files["uploadpic3"];
string Extension = Path.GetExtension(file.FileName);
string newFileName = "3" + Extension;
if (!Extension.Equals(".gif"))
{
ModelState.AddModelError("validationLogoError3", "只能上传gif类型图片");
}
else
{
file.SaveAs(Path.Combine(Server.MapPath("/Content/Logo/"), newFileName));
return Content("<script >alert('保存成功!');location.href='logo';</script >");
}
}
break;
}
case "保存4":
{
if (Request.Files.Count != 0)
{
HttpPostedFileBase file = Request.Files["uploadpic4"];
string Extension = Path.GetExtension(file.FileName);
string newFileName = "4" + Extension;
if (!Extension.Equals(".gif"))
{
ModelState.AddModelError("validationLogoError4", "只能上传gif类型图片");
}
else
{
file.SaveAs(Path.Combine(Server.MapPath("/Content/Logo/"), newFileName));
return Content("<script >alert('保存成功!');location.href='logo';</script >");
}
}
break;
}
}
return View();
}
该方法只是让大家知道
如何在一个MVC页面中实现多个提交按钮的思路,代码并没有经过优化,大家在实际操作中可将保存图片的方法提取出来,以减少代码量。P