public async Task <IActionResult> DownloadImage(string imageId) { if (string.IsNullOrEmpty(imageId)) { return(this.BadRequest()); } Data.Models.Image imageInfo = this.imagesService.GetImageById <Data.Models.Image>(imageId); if (imageInfo == null) { return(this.BadRequest()); } using (System.Drawing.Image sourceImage = await this.GetImageFromUrl(imageInfo.ImageUrl)) { if (sourceImage != null) { try { Stream outputStream = new MemoryStream(); sourceImage.Save(outputStream, sourceImage.RawFormat); outputStream.Seek(0, SeekOrigin.Begin); return(this.File(outputStream, System.Net.Mime.MediaTypeNames.Image.Jpeg, Path.GetFileName(imageInfo.ImageUrl))); } catch (Exception e) { this.logger.LogError(e, $"Error when send file from url:{imageInfo.ImageUrl}"); } } } return(this.NotFound()); }
internal static Image ToViewModel(Data.Models.Image image) { return(new Image() { Id = image.Id, ImageBase64Data = Convert.ToBase64String(image.ImageData), ImageTitle = image.ImageTitle, ProductId = image.ProductId, Type = image.Type }); }
public virtual ActionResult FileUpload(HttpPostedFileBase file) { try { IImagesRepository imagesRepository = DependencyResolver.Current.GetService <IImagesRepository>(); var memStream = new MemoryStream(); file.InputStream.CopyTo(memStream); byte[] fileData = memStream.ToArray(); //get existing Data.Models.Image newImage = imagesRepository.GetList().FirstOrDefault(x => x.Name == file.FileName && x.Binary.Length == fileData.Length); //get from database if (newImage == null) { newImage = imagesRepository.Insert(new Data.Models.Image { Binary = fileData, Name = file.FileName, CreatedDateTime = DateTime.Now }); } return(Json(new { success = true, response = "File uploaded.", id = newImage.Id.ToString(), fileName = file.FileName })); } catch (Exception exception) { return(Json(new { success = false, response = exception.Message })); } }
public IActionResult Upload() { try { var imagesPath = Path.Combine(this.hostingEnvironment.ContentRootPath, GlobalConstants.PathToUploadImages.FixOsPath()); var datePath = string.Format(@"{0}\{1}\".FixOsPath(), DateTime.Today.Year, DateTime.Today.Month); var path = Path.Combine(imagesPath, datePath); var file = this.Request.Form?.Files.FirstOrDefault(); if (file == null || file.Length == 0) { throw new InvalidDataException("Upload 1 file"); } var type = file.ContentType.Split('/'); var fileName = Guid.NewGuid().ToString(); using var sharpImage = Image.Load(file.OpenReadStream(), out IImageFormat format); var width = sharpImage.Width; var height = sharpImage.Height; var dpi = sharpImage.Metadata.HorizontalResolution; // This value is not always correct !!! if (sharpImage.Metadata.ResolutionUnits == SixLabors.ImageSharp.Metadata.PixelResolutionUnit.PixelsPerMeter) { dpi = sharpImage.Metadata.HorizontalResolution * 0.0254; } var isPng = format.Name.ToLower() == "png"; if (width == default(int) || height == default(int)) { throw new InvalidDataException("Image width or height is 0"); } if (dpi == default(double)) { throw new InvalidDataException("Image dpi is 0"); } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (isPng) { // png files can be transparent and should be converted to jpeg sharpImage.Mutate(x => x.BackgroundColor(new Rgba32(255, 255, 255))); sharpImage.Save(path + fileName + ".jpeg"); } else { using (var stream = new FileStream(path + fileName + "." + type[1], FileMode.Create)) { file.CopyTo(stream); } } var image = new Data.Models.Image() { Name = fileName, OriginalName = file.FileName, ContentType = isPng ? "image/jpeg" : file.ContentType, Path = datePath, Width = width, Height = height, Dpi = (int)Math.Ceiling(dpi) }; this.images.Add(image); var result = new { success = true, filename = image.Name, width = image.Width, height = image.Height, dpi = image.Dpi }; return(this.Json(result)); } catch (Exception ex) { var result = new { success = false, message = ex.Message }; this.loggerService.Log(Data.Models.LogLevel.Error, ex, "500"); return(this.Json(result)); } }