public IActionResult DetailsPhoto(int id, IFormFile file) // IFormFile for one Photo and IFormCollection for Multi Photo { var details = detailsRepo.GetById(id); if (details == null) { return(NotFound()); } var uploadFolderPath = Path.Combine(host.WebRootPath, "uploads"); if (!Directory.Exists(uploadFolderPath)) { Directory.CreateDirectory(uploadFolderPath); } var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var filePath = Path.Combine(uploadFolderPath, fileName); using (var stream = new FileStream(filePath, FileMode.Create)) { file.CopyTo(stream); } var photo = new PhotoModel { FileName = fileName }; // subDepartment.Photos.Add(photo); photo.DetailsId = details.Id; photoRepo.Add(photo); uow.Save(); var result = mapper.Map <PhotoModel, PhotoResource>(photo); return(Ok(result)); }
public JsonResult Upload(FormCollection collection) { var model = new PhotoEntry(); //获取上传文件队列 var oFile = Request.Files["Filedata"]; if (oFile != null) { string topDir = collection["folder"]; // 获取uploadify的folder配置,在此示例中,客户端配置了上传到 Files/ 文件夹 // 检测并创建目录:当月上传的文件放到以当月命名的文件夹中,例如2011年11月的文件放到网站根目录下的 /Files/201111 里面 string dateFolder = Utility.GetUploadBasePath(topDir) + @"/" + DateTime.Now.Date.ToString("yyyyMM"); string thumbnailFolder = dateFolder + @"/thumbnail"; if (!Directory.Exists(dateFolder)) // 检测是否存在磁盘目录 { Directory.CreateDirectory(dateFolder); // 不存在的情况下,创建这个文件目录 例如 C:/wwwroot/Files/201111/ } if (!Directory.Exists(thumbnailFolder)) { Directory.CreateDirectory(thumbnailFolder); } // 使用Guid命名文件,确保每次文件名不会重复 string guidFileName = Guid.NewGuid() + Path.GetExtension(oFile.FileName).ToLower(); // 保存文件,注意这个可是完整路径,例如C:/wwwroot/Files/201111/92b2ce5b-88af-405e-8262-d04b552f48cf.jpg var originalPath = dateFolder + @"/" + guidFileName; oFile.SaveAs(originalPath); var original = new DirectoryInfo(originalPath).FullName.Replace(AppDomain.CurrentDomain.BaseDirectory, ""); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// TODO 在此,您可以添加自己的业务逻辑,比如保存这个文件信息到数据库 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// var thumbnailPath = thumbnailFolder + @"/" + guidFileName; using (var file = System.IO.File.OpenRead(originalPath)) { PhotoUtils.CutForCustom(file, thumbnailPath, 380, 252, 50); } string thumbnail = new DirectoryInfo(thumbnailPath).FullName.Replace(AppDomain.CurrentDomain.BaseDirectory, ""); var newsId = DataCast.Get <int>(collection["NewsId"]); model.Description = "请添加描述"; model.NewsId = newsId; model.Original = @"~\" + original; model.Thumbnail = @"~\" + thumbnail; _photo.Add(model); } return(Json(model)); }
public async Task <IActionResult> ReplacePhoto(Photos model) { if (model != null) { string FileExtension = String.Empty; List <IFormFile> filelst = new List <IFormFile>(); filelst.Add(model.ReplacePhoto); //CHECK for new file if it's valid then proceed if (!ValidateFileExtension(filelst, out FileExtension)) { TempData["Error"] = "File Extension " + FileExtension + " is not allowed"; return(RedirectToAction("AlbumDetail", new RouteValueDictionary(new { controller = "User", action = "AlbumDetail", albumId = model.AlbumEncrypted }))); } Photos p = photoRepository.DeletePhoto(model.Id); //return RedirectToAction("AlbumDetail", new RouteValueDictionary(new { controller = "User", action = "AlbumDetail", albumId = AlbumEncrypted })); LMCommon.DeletePhoto(model.PhotoPath, hostingEnvironment.WebRootPath, configuration["PhotoPath"]); string path = await LMCommon.UploadPhoto(model.ReplacePhoto, hostingEnvironment.WebRootPath, configuration["PhotoPath"]); //REPLACE if (!String.IsNullOrEmpty(path)) { AccountUser user = await GetLoggedInUser(); Photos ph = new Photos(); ph.PhotoPath = path; ph.AlbumId = model.AlbumId; ph.UserId = user.Id; ph.UplodedDate = DateTime.Now.ToString(); ph.ImgName = GetImageName(path); photoRepository.Add(ph); toastNotification.AddSuccessToastMessage("Image Replace successfully"); } } return(RedirectToAction("AlbumDetail", new RouteValueDictionary(new { controller = "User", action = "AlbumDetail", albumId = model.AlbumEncrypted }))); }
/// <summary> /// 保存添加数据 Add(PhotoInfo entity) /// </summary> /// <param name="entity">实体类(PhotoInfo)</param> ///<returns>返回新增的ID</returns> public void Add(PhotoInfo entity) { _photo.Add(entity); }