コード例 #1
0
ファイル: HomeController.cs プロジェクト: xiaowoc/test
 public IActionResult Creat(StudentCreatViewModel model)
 {
     if (ModelState.IsValid)
     {
         string uniqueFileName = null;
         //if (model.Photos != null || model.Photos.Count > 0)
         //{
         //    //string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");//wwwroot下的images
         //    //uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;//特殊唯一文件名
         //    //string filePath = Path.Combine(uploadsFolder, uniqueFileName);//拼接出文件目录文件名
         //    //model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));//复制图片到指定目录
         //    foreach (var Photo in model.Photos)
         //    {
         //        string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");//wwwroot下的images
         //        uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName;//特殊唯一文件名
         //        string filePath = Path.Combine(uploadsFolder, uniqueFileName);//拼接出文件目录文件名
         //        Photo.CopyTo(new FileStream(filePath, FileMode.Create));//复制图片到指定目录
         //    }
         //}
         uniqueFileName = ProcessUploadedFile(model);
         Student newstudent = new Student {
             Id = model.Id, Name = model.Name, Number = model.Number, PhotoPath = uniqueFileName
         };
         _studentRepository.Add(newstudent);
         return(RedirectToAction("Details", new { id = newstudent.Id }));
         //Student newstudent = _studentRepository.Add(student);
         //return RedirectToAction("Details", new { id = newstudent.Id });
     }
     return(View());
 }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: xiaowoc/test
        /// <summary>
        /// 拼接图片存放路径,复制图片到指定路径
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private string ProcessUploadedFile(StudentCreatViewModel model)
        {
            string uniqueFileName = null;

            if (model.Photos != null || model.Photos.Count > 0)
            {
                //string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");//wwwroot下的images
                //uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;//特殊唯一文件名
                //string filePath = Path.Combine(uploadsFolder, uniqueFileName);//拼接出文件目录文件名
                //model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));//复制图片到指定目录
                foreach (var Photo in model.Photos)
                {
                    string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); //wwwroot下的images
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName;             //特殊唯一文件名
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);                 //拼接出文件目录文件名
                    using (var copyFilePath = new FileStream(filePath, FileMode.Create))
                    {
                        Photo.CopyTo(copyFilePath);//复制图片到指定目录
                    }
                }
            }
            return(uniqueFileName);
        }