码迷,mamicode.com
首页 > Web开发 > 详细

MVC上传图片

时间:2020-02-13 16:31:22      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:数据   view   data   res   file   upload   img   app   title   

//控制器
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="上传" />
}

 

 

MVC上传图片

标签:数据   view   data   res   file   upload   img   app   title   

原文地址:https://www.cnblogs.com/ly-03-04/p/12303904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!