public async Task <UploadedFileResultDto> Upload(IFormFile file, string uploadedLocationName) { if (file.Length == 0 || file == null) { throw new RestException(HttpStatusCode.BadRequest, new { EmtyFile = "Litfen Dosya Seçiniz.." }); } var fileExtensions = Path.GetExtension(file.FileName); string[] accepted_file_types = new[] { ".jpg", ".jpeg", ".png", ".JPG", ".JPEG", ".PNG", ".GIF", ".gif" }; if (!accepted_file_types.Any(s => s == fileExtensions)) { throw new RestException(HttpStatusCode.BadRequest, new { InCorrectFileTypes = "Yüklenen dosya tipi desteklenmiyor.Sadece .jpg,.jpeg,.png desteklenmektedir." }); } var allowedFileSize = 5 * 1024 * 1024; if (file.Length > allowedFileSize) { throw new RestException(HttpStatusCode.BadRequest, new { FileSizeExceed = "Dosya boyutu fotopraf için 5MB video için 40MB olmalıdır." }); } var uploadsFolderPath = Path.Combine(host.ContentRootPath + "/MyStaticFiles/images", uploadedLocationName); if (!Directory.Exists(uploadsFolderPath)) { Directory.CreateDirectory(uploadsFolderPath); } var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var filePath = Path.Combine(uploadsFolderPath, fileName); using (var image = new MagickImage(file.OpenReadStream())) { image.Write(filePath); } var imageToCompress = new FileInfo(host.ContentRootPath + "/MyStaticFiles/images" + "/" + uploadedLocationName + "/" + fileName); var optimizer = new ImageOptimizer(); optimizer.Compress(imageToCompress); imageToCompress.Refresh(); var result = new UploadedFileResultDto() { FullPath = config["ApiUrl"] + "images/" + uploadedLocationName + "/" + fileName, Name = "images/" + uploadedLocationName + "/" + fileName, FileType = "image" }; return(await Task.FromResult(result)); }
public async Task <List <UploadedFileResultDto> > UploadPdf(IFormFile file, string uploadedLocationName) { if (file.Length == 0 || file == null) { throw new RestException(HttpStatusCode.BadRequest, new { EmtyFile = "Litfen Dosya Seçiniz.." }); } var fileExtensions = Path.GetExtension(file.FileName); string[] accepted_file_types = new[] { ".jpg", ".jpeg", ".png", ".JPG", ".JPEG", ".PNG", ".pdf" }; if (!accepted_file_types.Any(s => s == fileExtensions)) { throw new RestException(HttpStatusCode.BadRequest, new { InCorrectFileTypes = "Yüklenen dosya tipi desteklenmiyor.Sadece .jpg,.jpeg,.png desteklenmektedir." }); } var allowedFileSize = 5 * 1024 * 1024; if (file.Length > allowedFileSize) { throw new RestException(HttpStatusCode.BadRequest, new { FileSizeExceed = "Dosya boyutu fotopraf için 5MB video için 40MB olmalıdır." }); } var uploadsFolderPath = Path.Combine(host.ContentRootPath + "/MyStaticFiles/images", uploadedLocationName); if (!Directory.Exists(uploadsFolderPath)) { Directory.CreateDirectory(uploadsFolderPath); } List <string> fileNames = new List <string>(); var settings = new MagickReadSettings(); settings.Density = new Density(300, 300); using (var image = new MagickImage(file.OpenReadStream())) { using (var images = new MagickImageCollection()) { images.Read(image.FileName, settings); var page = 1; foreach (var item in images) { var fileName = Guid.NewGuid().ToString() + ".png"; var filePath = Path.Combine(uploadsFolderPath, fileName); item.Write(filePath); fileNames.Add(fileName); page++; } } } List <UploadedFileResultDto> result = new List <UploadedFileResultDto>(); foreach (var fileName in fileNames) { var uploadResult = new UploadedFileResultDto() { FullPath = config["ApiUrl"] + "images/" + uploadedLocationName + "/" + fileName, Name = "images/" + uploadedLocationName + "/" + fileName, FileType = "image" }; result.Add(uploadResult); } return(await Task.FromResult(result)); }