public async Task <IActionResult> Create(Link link, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (image != null)
                    {
                        image.Check(1048576, new string[] { "image/jpg", "image/jpeg", "image/png", "image/gif" });
                        var imagePath = $"uploads/links/{PersianDateTime.Now.ToString("yyyy/MM/dd/yyyyMMddhhmmss")}{DateTime.Now.ToString("ffff") + new Random().Next(1000000, 9999999)}_{Helpers.File.ValidateName(image.FileName)}";
                        await _ifileManager.SaveFile(image, imagePath);

                        link.ImagePath = $"/{imagePath}";
                    }

                    _context.Add(link);
                    await _context.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    TempData["LinkCreateStatus"] = e.Message;
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(link));
        }
示例#2
0
        public async Task <IActionResult> OnPostSetProfileImageAsync(IFormFile image)
        {
            //This action is for users that have/have not profile image

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"کاربر با شناسه '{_userManager.GetUserId(User)}' یافت نشد."));
            }

            try
            {
                //Check Validation Of Image
                image.Check(1048576, new string[] { "image/jpg", "image/jpeg", "image/png", "image/gif" });

                //Generate Name & Path Of File
                var imageName = DateTime.Now.ToString("yyyyMMddhhmmssffff") + Path.GetExtension(image.FileName);
                var imagePath = Path.Combine("UserUploads/UsersProfile", imageName);

                //Store File In File System
                _ifileManager.SaveFile(image, imagePath);

                if (user.ProfileImagePath.EndsWith("default.png"))
                {
                    //user has not profile image

                    //Add Image Path To Model
                    user.ProfileImagePath = "/UserUploads/UsersProfile/" + imageName;
                    await _userManager.UpdateAsync(user);
                }
                else
                {
                    //user has profile image

                    //Delete Old Image
                    _ifileManager.DeleteFile(user.ProfileImagePath);

                    //Update Record To Database
                    user.ProfileImagePath = "/UserUploads/UsersProfile/" + imageName;
                    await _userManager.UpdateAsync(user);
                }
                StatusMessage = "عکس نمایه شما تنظیم گردید.";
            }
            catch (Exception e)
            {
            }
            return(RedirectToPage());
        }
        public async Task <IActionResult> Edit(short id, IFormFile image, Link linksModel)
        {
            if (id != linksModel.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var link = await _context.Links.FindAsync(id);

                    if (link == null)
                    {
                        return(NotFound());
                    }

                    if (image != null)
                    {
                        image.Check(1048576, new string[] { "image/jpg", "image/jpeg", "image/png", "image/gif" });
                        var imagePath = $"uploads/links/{PersianDateTime.Now.ToString("yyyy/MM/dd/yyyyMMddhhmmss")}{DateTime.Now.ToString("ffff") + new Random().Next(1000000, 9999999)}-{Helpers.File.ValidateName(image.FileName)}";

                        //Upload new image
                        await _ifileManager.SaveFile(image, imagePath);

                        //Remove old image
                        _ifileManager.DeleteFile(link.ImagePath);
                        //Set new image
                        link.ImagePath = $"/{imagePath}";
                    }

                    link.IsExpanded = linksModel.IsExpanded;
                    link.Title      = linksModel.Title;
                    link.Url        = linksModel.Url;

                    _context.Update(link);
                    await _context.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    TempData["LinkEditStatus"] = e.Message;
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(linksModel));
        }