asp.net mvc V与C图片的交互方式罗列
1.上传图片
a.cshtml
<div class="container-fluid">
@using (Html.BeginForm("Add", "Dodate", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken("doutu")
@Html.ValidationSummary(true)
@Html.HiddenFor(m => m.id)
<div class="control-group">
<label class="control-label">
上传原型图:</label>
<div class="controls">
<input type="file" id="file" name="file" required />
</div>
</div>
}
</div>
b.controller
[HttpPost]
[ValidateInput(true)]
[ValidateAntiForgeryToken(Salt = "doutu")]
public ActionResult Add()
{
HttpFileCollectionBase httpFiles = HttpContext.Request.Files;
HttpPostedFileBase file = httpFiles[0];
string pathName = string.Format("/Image/{0}", DateTime.Now.ToString("yyyyMMddhhmmssfff") + file.FileName);
string filename = Server.MapPath("~/" + pathName);
file.SaveAs(filename);
return RedirectToAction("Index");
}
MVC 里面 View层如何获取Controller 层的图片,以显示在界面上,上代码
访问这个路径就可以了。
[HttpGet]
[HttpGet]
public ActionResult TestImage(string words1) { string imageSrc = HttpContext.Server.MapPath("~/Image/dout1/2.png"); byte[] image = Utils.DrawImage(imageSrc, words1); return File(image, "image/jpeg"); }
[HttpPost]
[HttpPost]
[ValidateInput(true)]
[ValidateAntiForgeryToken()]
public ActionResult post(DSet dSet)
{
string imageSrc = HttpContext.Server.MapPath("~/" + dSet.sourceSrc);
byte[] image = Utils.DrawImage(imageSrc);
ViewBag.image = image;
return View(dSet);
}
post 的页面对应如下
<div style="text-align: center">
@{
var base64 = Convert.ToBase64String(ViewBag.image);
var imgSrc = String.Format("data:image/jpeg;base64,{0}", base64);
}
<img id="image" style="max-width: 100%" src="@imgSrc" />
</div>

浙公网安备 33010602011771号