How to upload profile picture and update it in identity asp.net core 3



  • I want to upload profile picture in identity user and update it in account management. if there is any post with good examples for asp.net core please give me links.



  • I did it myself with FileForm Method. First You Have to Add string property in User Class(https://docs.microsoft.com/en-us/aspnet/core/security/authentication/add-user-data?view=aspnetcore-3.0&tabs=visual-studio).

            public string Avatar { get; set; }
    
    

    and then, update the Manage/Index files by following the documentation. Now, How to create the file upload system? I did like the code below. but if i need to change something for security, please don't forget to help.

                        if (Input.Avatar != null)
                    {
                        string existfile = Path.Combine(hostingEnvironment.WebRootPath, "uploads", user.Avatar);
                        System.IO.File.Delete(existfile);
    
                    }
                    var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads", "avatar");
                    var filePath = Path.Combine(uploads, fileName);
                    this.Image.CopyTo(new FileStream(filePath, FileMode.Create));
                    user.Avatar = fileName; // Set the file name
    
    

    GetUniqueFileName Class after the PostAsync class:

            private string GetUniqueName(string fileName)
        {
            fileName = Path.GetFileName(fileName);
            return Path.GetFileNameWithoutExtension(fileName)
                   + "_" + Guid.NewGuid().ToString().Substring(0, 4)
                   + Path.GetExtension(fileName);
        }
    
    

    You also have to add the IWebHostEnvironment dependency injection and update the cshtml form with multipart/form-data enctype. Don't forget to follow the .net documentation rules also. Good Luck!



最新内容

  • V
    问题

    When user swipe the cell I m showing some option. Everything is working fine, When user scroll the table swipe option goes off. I want the same behaviour when I reload the table on data change. How to achieve that. If user has swipe the cell and I reload table , Swipe options are still there.

    回答1:

    0

    You hide the swiped cell just add this line of code before reloading the table view.

    tableView.setEditing(false, animated: true)

    Now for enabling the editing, In your cellForAtIndex Path function make a check like

    if indexPath.row == array.size { tableView.setEditing(true, animated: true) }

    share|improve this answer

    answered Sep 13 at 13:12

    AbhishekAbhishek 21422 silver badges1010 bronze badges

    add a comment |

    read more
  • You hide the swiped cell just add this line of code before reloading the table view.

    tableView.setEditing(false, animated: true)

    Now for enabling the editing, In your cellForAtIndex Path function make a check like

    if indexPath.row == array.size { tableView.setEditing(true, animated: true) }

    read more
  • V

    When user swipe the cell I m showing some option. Everything is working fine, When user scroll the table swipe option goes off. I want the same behaviour when I reload the table on data change. How to achieve that. If user has swipe the cell and I reload table , Swipe options are still there.

    read more

最新主题

推荐阅读

本站部分内容来自互联网,其发布内容言论不代表本站观点,如果其链接、内容的侵犯您的权益,烦请联系我们,我们将及时予以处理。
Powered by NodeBB | 备案号:宁ICP备15000671号