MVC 上传文件

                if (file != null)
                {
                    string path = Server.MapPath("~/Uploads/"); //获得服务器物理路径
                    if (!System.IO.Directory.Exists(path))//判断目录是否存在
                    {
                        System.IO.Directory.CreateDirectory(path);//创建目录
                    }
                    file.SaveAs(path + System.IO.Path.GetFileName(file.FileName)); //获得文件名和扩展名
                }
                if (file.ContentLength > 0)//获取上传文件的大小,以字节为单位
                {
                    string FileName = System.IO.Path.GetFileName(file.FileName);//返回文件名+扩展名
                    string path = System.IO.Path.Combine(Server.MapPath("/bin"), file.FileName); //将两个字符串组合成一个路径
                    file.SaveAs(path);
                }
 [HttpPost]
        public ActionResult Index(IEnumerable<HttpPostedFileBase> files) //接收多个文件
        {
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = System.IO.Path.GetFileName(file.FileName);
                    var path = System.IO.Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
                }
            }
            return RedirectToAction("Index");
        }
    <div> 
@*上传单个文件*@
        <form action="/Home/UpdateFile2" enctype="multipart/form-data" method="post" id="form1">
            <input type="file" name="file" /><!--只能选择单个文件--->
            <input type="button" name="Submit" id="Submit" value="Upload" />
        </form> 

 @*上传多个文件*@
        <form action="/Home/UpdateFile1" enctype="multipart/form-data" method="post">
            <input type="file" name="img" multiple="multiple" /> <!--可选择多个文件-->
        </form>
    </div>
      public ActionResult Upload(HttpPostedFileBase[] fileToUpload)
        {
            foreach (HttpPostedFileBase file in fileToUpload)
            {
                string path = System.IO.Path.Combine(Server.MapPath("~/App_Data"), System.IO.Path.GetFileName(file.FileName));
                file.SaveAs(path);
            }

            ViewBag.Message = "File(s) uploaded successfully";
            return RedirectToAction("Index");
        }
        [HttpPost]
        public bool UploadFiles()
        {
            // 文件数为0证明上传不成功
            if (Request.Files.Count == 0)
            {
                throw new Exception("请选择上传文件!");
            }

            string uploadPath = Server.MapPath("../UploadFiles/");

            // 如果UploadFiles文件夹不存在则先创建
            if (!System.IO.Directory.Exists(uploadPath))
            {
                System.IO.Directory.CreateDirectory(uploadPath);
            }

            // 保存文件到UploadFiles文件夹
            for (int i = 0; i < Request.Files.Count; ++i)
            {
                HttpPostedFileBase file = Request.Files[i];
                // 文件名为空证明没有选择上传文件
                if (file.FileName == "")
                {
                    return false;
                }

                string filePath = uploadPath + System.IO.Path.GetFileName(file.FileName);
                string fileName = file.FileName;

                // 检查上传文件的类型是否合法
                string fileExtension = System.IO.Path.GetExtension(filePath).ToLower();
                string fileFilter = System.Configuration.ConfigurationManager.AppSettings["FileFilter"];
                if (fileFilter.IndexOf(fileExtension) <= -1)
                {
                    Response.Write("对不起!请上传图片!!");
                    return false;
                }

                // 如果服务器上已经存在该文件则要修改文件名与其储存路径
                while (System.IO.File.Exists(filePath))
                {
                    Random rand = new Random();
                    fileName = rand.Next().ToString() + "-" + file.FileName;
                    filePath = uploadPath + System.IO.Path.GetFileName(fileName);
                }
                // 把文件的存储路径保存起来
                //SaveUploadFileInfo(fileName, filePath);
                // 保存文件到服务器
                file.SaveAs(filePath);
            }

            return true;
        }
<appSettings>
    ...
    <add key="FileFilter" value=".gif|.jpg|.jpeg|.png|" />
    ...
  </appSettings>

  

原文地址:https://www.cnblogs.com/enych/p/8351388.html