MVC上传图片

//控制器
using
System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.IO;//上传文件的数据流 namespace UploadImg.Controllers { public class UploadController : Controller { // GET: Upload public ActionResult Add() { return View(); } [HttpPost] public void Add(HttpPostedFileBase imgFile) { //判断是否上传图片 if (imgFile!=null) { //虚拟路径 var p = "/Content/Imgs/" + Path.GetFileName(imgFile.FileName); imgFile.SaveAs(Server.MapPath(p)); } } } }

//页面


@{
ViewBag.Title = "Add";
}


<h2>Add</h2>
@*enctype = "multipart/form-data"上传文件必须的属性*@
@using (Html.BeginForm("Add", "Upload", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<input id="File1" type="file" name="imgFile" />
<input id="Submit1" type="submit" value="上传" />
}

 

 

posted @ 2020-02-13 16:01  噜啦啦0001  阅读(167)  评论(0编辑  收藏  举报