//view:修改form表单的样式
<% Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }); %>
<input id="HeadImage" type="file" style="width:150px;" runat="server" />
<% Html.EndForm(); %>
Controller:
//设置上传路径
string path = Server.MapPath("../HeadImages");
//获取file文本
foreach (string file in HttpContext.Request.Files)
{
var fileBase = HttpContext.Request.Files[file];
try
{
if (fileBase.ContentLength > 0)
{
string fileName = fileBase.FileName;
string extentName = fileName.Substring(fileName.LastIndexOf("."));
string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssms") + extentName;
fileBase.SaveAs(path + "//" + newFileName);
return Content("<script>alert('上传成功!');window.location.href = '../Consultant/Index';</script>");
}
}
catch (Exception e)
{
throw;
}
}
return view();