Exemplo n.º 1
0
        public string UploadLogo(UploadedLogo logo)
        {
            Photo photo = new Photo
            {
                UserID      = logo.UserId,
                Title       = logo.Title,
                Description = logo.Description,
                CreatedDate = DateTime.Now,
                URL         = logo.LogoData
            };

            _homeDA.UploadLogo(photo);
            return("success");
        }
Exemplo n.º 2
0
        public ActionResult UploadLogo(UploadedLogo data)
        {
            string extension;

            // Searching for extension and remove description (unnecessary part from decoded image)
            if (data.LogoData.StartsWith("data:image/jpeg;base64,"))
            {
                extension     = "jpg";
                data.LogoData = data.LogoData.Substring(23);
            }
            else if (data.LogoData.StartsWith("data:image/png;base64,"))
            {
                extension     = "png";
                data.LogoData = data.LogoData.Substring(22);
            }
            else if (data.LogoData.StartsWith("data:image/gif;base64,"))
            {
                extension     = "gif";
                data.LogoData = data.LogoData.Substring(22);
            }
            else
            {
                return(Json("fail"));
            }

            // write picture in ~/Uploads/Logos/ : format {DateTime.Now.Ticks_ActiveUser.id}
            string imageName         = DateTime.Now.Ticks + "_" + HttpContext.Session["ActiveUserID"] + "." + extension;
            string imageRelativePath = @"/Uploads/Logos/" + imageName;
            string imagePath         = AppDomain.CurrentDomain.BaseDirectory + imageRelativePath;

            // Converting Base64 string to bytes and write in file!!!
            var photoBytes = Convert.FromBase64String(data.LogoData);

            using (var photoFile = new FileStream(imagePath, FileMode.Create))
            {
                photoFile.Write(photoBytes, 0, photoBytes.Length);
                photoFile.Flush();
            }

            // to write this path in DB
            data.LogoData = imageRelativePath;

            string response = _homeBL.UploadLogo(data);

            return(Json(response));
        }