public IActionResult UploadProfilImg(FileUploadCreateModel model) { if (model == null) { throw new ApiException("Błąd parametrów"); } var file = _fileService.Upload(model); if (file == null) { throw new ApiException("Bład"); } return(new ResponseObjectResult(file)); }
public FileUploadViewModel Upload(FileUploadCreateModel fileUploadViewModel) { if (fileUploadViewModel.File.Length < 0) { return(null); } string _path = Path.Combine(_env.WebRootPath, "uploadFiles"); if (!Directory.Exists(_path)) { Directory.CreateDirectory(_path); } else { foreach (FileInfo file in (new DirectoryInfo(_path)).EnumerateFiles()) { file.Delete(); } } var fileName = (new Random()).Next() + "." + Path.GetExtension(fileUploadViewModel.File.FileName).Substring(1); using (var fs = new FileStream(Path.Combine(_path, fileName), FileMode.Create)) { fileUploadViewModel.File.CopyTo(fs); } return(new FileUploadViewModel() { Extension = Path.GetExtension(fileName).Substring(1), Height = fileUploadViewModel.Height, Size = fileUploadViewModel.Size, Source = $"/uploadFiles/{fileName}" }); }