Ejemplo n.º 1
0
        public void InsertPictures(int itemId, int userId, ItemCreateEditModel itemModel, ItemReturnModel itemReturnModel)
        {
            var pictureDirectory = _configuration.GetConnectionString("PictureDirectory");

            foreach (var picture in itemModel.Pictures.Select(x => x.PictureString).ToList())
            {
                Guid   guid = Guid.NewGuid();
                string pictureFileName = "", path = "", pictureType = "", extension = "";

                //var pictureString = picture.Replace("data:image/png|tiff|jpg|gif;base64,", "");

                var pictureString = picture.Split(',')[1];

                //pictureString = picture.ToString();
                pictureType     = picture.Substring(0, picture.IndexOf(";"));
                pictureType     = pictureType.Replace("data:", "");
                extension       = GetExtensionFromMimeType(pictureType);
                pictureFileName = Convert.ToString(guid) + extension;

                //string converted = pictureString.Replace('-', '+');
                //converted = converted.Replace('_', '/');

                byte[] pictureBytes = Convert.FromBase64String(pictureString);

                //Store Image in azure Blob
                var _task = Task.Run(() => this.UploadFileToBlobAsync(pictureFileName, pictureBytes, pictureType, pictureDirectory));
                _task.Wait();
                path = _task.Result;


                //store image at location
                //string path = Path.Combine(_hostingEnvironment.WebRootPath, "Pictures", pictureFileName);
                //System.IO.File.WriteAllBytesAsync(path, pictureBytes);

                Pictures picturesEntity = new Pictures()
                {
                    ItemId = itemId,
                    //Binary = pictureBytes.ToArray(),
                    Name      = pictureFileName,
                    Path      = path,
                    CreatedBy = userId
                };

                itemReturnModel.PicturesPath.Add(path);

                _picturesService.InsertPictures(picturesEntity);
            }
        }