超简单实现MVC上传文件

版权声明:biubiubiu https://blog.csdn.net/a_lllll/article/details/89916360

超简单实现MVC上传文件


Action方法不能重载,【注意】方法名相同的最多允许出现两次(这两次指的是两种提交方式:GET和POST)


上传文件的参数类型为:HttpPostedFileBase
【注意】上传文件的form表单中一定要加:enctype=“multipart/form-data”


控制器代码

		[HttpGet]
        public ActionResult UpLoadFile()
        {
            return View();
        }
        [HttpPost]
        public ActionResult UpLoadFile(HttpPostedFileBase file)
        {
            //注意:下面的UpLoad是自己创建的一个文件夹
            var fileName = file.FileName;
            //方法一:
            //var filePath = Server.MapPath(string.Format("~/{0}", "UpLoad"));
            //file.SaveAs(Path.Combine(filePath, fileName));
            //方法二:
            file.SaveAs(Server.MapPath("~/UpLoad/" + fileName));
            return View();
        }

视图层代码

		<form action="~/UploadFile/UploadFile" method="post" enctype="multipart/form-data">
            <input type="file" name="file" /><br />
            <input type="submit" value="提交" />
        </form>

猜你喜欢

转载自blog.csdn.net/a_lllll/article/details/89916360