首页 > 代码库 > 上传图片后台写法

上传图片后台写法

[HttpPost]

        public ActionResult UpFile()

        {

 

 

            int count = Request.Files.Count;

 

 

            for (int i = 0; i < count; i++)

            {

                WebTest.Entity.FileInfo fileInfo = new WebTest.Entity.FileInfo();

                HttpPostedFileBase file = Request.Files[i];

                string[] NameTpye = file.FileName.Split(new char[] { ‘.‘ });

          

                string Path = "/Image/UpWord/" + DateTime.Now.ToString("yyyyMMddHHmmssffff");

                string FilePath = Server.MapPath(Path);

                if (!Directory.Exists(FilePath))

                {

                    Directory.CreateDirectory(FilePath);

                }

                file.SaveAs(FilePath + "/" + file.FileName);

 

                fileInfo.FileName = NameTpye[0];

                fileInfo.FileType = "." + NameTpye[1];

                fileInfo.FileUrl = Path + "/" + file.FileName;

                fileInfo.Refnum = 1;

                fileInfo.RefnumTable = "News";

                fileInfo.Size = (file.ContentLength / 1024).ToString();

 

                db.Set<WebTest.Entity.FileInfo>().Add(fileInfo);

 

            }

            int res = db.SaveChanges();

 

            if (res > 0)

            {

                return Content("OK:上传文件成功!");

            }

            else

            {

                return Content("NO:上传文件失败!");

            }

        }

上传图片后台写法