MVC上传图片
---MVC控制器
/// <summary>
/// 上传(添加后台)
/// </summary>
/// <returns></returns>
///
[HttpPost]
public ActionResult shangchaun(string Name, string Infotype, HttpPostedFileBase ExcFile)
{
if (ExcFile == null)
{
return View();
}
string strfileName = Server.MapPath("/EXCEL/"); //上传的路径 strfileName路径名
if (!Directory.Exists(strfileName))
{
Directory.CreateDirectory(strfileName);
}
string fName = Path.GetFileName(ExcFile.FileName); //获取上传时的文件名
string fNewName = string.Format("{0}.{1}",Guid.NewGuid().ToString(),fName.Split('.')[1]);//获取一个不重复的文件名 保存到数据库
ExcFile.SaveAs(strfileName+ fNewName);//保存到本地
InfoM m = new InfoM();
m.Name = Name;
m.Infotype = Infotype;
m.SCName = fName;
m.SCNewName = fNewName;
var result = bll.addinfo(m);
if (result > 0)
{
return Content("<script>alert('上传成功');location.href='showinfo'</script>");
}
else
{
return Content("<script>alert('上传失败')</script>");
}
}
---MVC View页面
//上传页面
<form action="@Url.Action("shangchaun")" method="post" enctype="multipart/form-data">
名字:<input id="Text1" type="text" name="Name" /><br />
类型:<input id="Text2" type="text" name="Infotype" /><br />
<input id="File1" type="file" name="ExcFile" />
<input id="Submit1" type="submit" value="上传" />
</form>