MVC控制器中代码
index.cshtml
<form action="/Home/Upload" method="post" enctype="multipart/form-data"> <input type="file" name="File" /> <button type="submit">方式一上传</button> </form> <hr /> <form action="/Home/UploadFile" method="post" enctype="multipart/form-data"> <input type="file" name="fileName" /> <button type="submit">方式二上传</button> </form>
控制器代码
//文件上传方式一 public ActionResult UpLoad() { Request.Files["File"].SaveAs(Request.MapPath("~/upload/") + Request.Files["File"].FileName); int fileCount = Request.Files.Count; //上传数量 double fileSize = Request.Files["File"].ContentLength; //文件大小(字节) string fileName = Request.Files["File"].FileName; //文件名 string fileType = Request.Files["File"].ContentType;//文件类型 string fileExt = System.IO.Path.GetExtension(fileName); //文件扩展后缀名 return Content($"上传数量:{fileCount} 文件名:{fileName} 文件类型:{fileType} 文件格式:{fileExt}"); } //文件上传方式二 public ActionResult UploadFile(HttpPostedFileBase fileName) { fileName.SaveAs(Request.MapPath("~/upload/") + fileName); return Content("OK"); }