Пример #1
0
        /*
         * helper for above
         */
        private string SaveFile(string folder, object sender2)
        {
            Uploader that = sender2 as Uploader;

            string fileName = Page.Server.MapPath(folder.Trim('/') + "/" + that.GetFileName());

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            byte[] content = Convert.FromBase64String(that.GetFileRawBASE64());
            using (FileStream stream = File.Create(fileName))
            {
                stream.Write(content, 0, content.Length);
            }
            return(folder.Trim('/') + "/" + that.GetFileName());
        }
Пример #2
0
        public IActionResult UploadAvatar(int EmployeeId)
        {
            string[] validExts = new string[] { ".jpeg", ".jpg", ".png" };
            var      files     = Request.Form.Files;

            if (files == null || files.Count != 1)
            {
                throw new NullReferenceException();
            }
            var file          = files.FirstOrDefault();
            var filename      = Uploader.GetFileName(file);
            var fileExtension = Path.GetExtension(filename).ToLower();

            if (validExts.Contains(fileExtension))
            {
                var newFileName = Guid.NewGuid().ToString("N") + fileExtension;
                Uploader.Upload(file, AVATAR_FOLDER_FULL_PATH, newFileName);

                var url = $"/{AVATAR_FOLDER}/{newFileName}";

                try
                {
                    _employeeService.UpdateAvatar(EmployeeId, url);
                    return(Ok());
                }
                catch (Exception e)
                {
                    System.IO.File.Delete(Path.Combine(AVATAR_FOLDER_FULL_PATH, newFileName));
                    throw e;
                }
            }
            else
            {
                throw new FileLoadException("Invalid File Extension");
            }
        }