概述
【第一种方式】 HttpPostedFileBase file
1、前端POST方式提交表单
@using (Html.BeginForm("Upload", "Test", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<br />
<input id="ButtonUpload" type="submit" value="提交" />
}
2、后台[HttpPost]接收
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
var fileName = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName));
file.SaveAs(fileName); //保存到这个路径
string src = "../Upload/" + Path.GetFileName(file.FileName);
Session["src"] = src; //跨页面传值
Session.Timeout = 10;
return RedirectToAction("Show");
//Request.MapPath("~/Upload")|C:UsersAdministratorDocumentsVisual Studio 2015ProjectsImageUploadImageUploadUpload
//Path.GetFileName(file.FileName)|c.jpg
//fileName|C:UsersAdministratorDocumentsVisual Studio 2015ProjectsImageUploadImageUploadUploadc.jpg
}
3、后台保存之后通过另一个页面显示
public ActionResult Show(string src)
{
ViewBag.src = Session["src"];
return View();
}
<img src=@Session["src"] alt="img" /> //或者用ViewBag.src
【第二种方式】ajax
图片在前台转换成流,通过ajax发送到后台,后台接收保存,或者先处理图片再保存【第一种方式】 HttpPostedFileBase file
1、前端POST方式提交表单
@using (Html.BeginForm("Upload", "Test", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<br />
<input id="ButtonUpload" type="submit" value="提交" />
}
2、后台[HttpPost]接收
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
var fileName = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName));
file.SaveAs(fileName); //保存到这个路径
string src = "../Upload/" + Path.GetFileName(file.FileName);
Session["src"] = src; //跨页面传值
Session.Timeout = 10;
return RedirectToAction("Show");
//Request.MapPath("~/Upload")|C:UsersAdministratorDocumentsVisual Studio 2015ProjectsImageUploadImageUploadUpload
//Path.GetFileName(file.FileName)|c.jpg
//fileName|C:UsersAdministratorDocumentsVisual Studio 2015ProjectsImageUploadImageUploadUploadc.jpg
}
3、后台保存之后通过另一个页面显示
public ActionResult Show(string src)
{
ViewBag.src = Session["src"];
return View();
}
<img src=@Session["src"] alt="img" /> //或者用ViewBag.src
【第二种方式】ajax
图片在前台转换成流,通过ajax发送到后台,后台接收保存,或者先处理图片再保存
最后
以上就是忧虑花瓣为你收集整理的asp.net mvc5 与图片的全部内容,希望文章能够帮你解决asp.net mvc5 与图片所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复