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!



最新内容

  • 问题

    I\'m creating Django forms using model forms because u I wanted the forms to be created automatically, but when I created this code the forms do not appear in the index.html page

    models.py

    <pre class="\"lang-py" prettyprint-override="">``` from django.db import models class BaseCase(models.Model): base_case_name = models.CharField(primary_key=True, max_length=255) version = models.TextField(blank=True, null=True) default = models.TextField(blank=True, null=True) # This field type is a guess. class Meta: managed = False db_table = \'base_case\' forms.py

    <pre class=""lang-py" prettyprint-override="">```
    from django import forms
    from SFP.models import *

    class BaseCaseForm(forms.ModelForm):
    class Meta :
    model = BaseCase
    fields='all'

    views.py

    <pre class="\"lang-py" prettyprint-override="">``` from django.shortcuts import render,redirect from .models import * from .forms import * def addbc(self, request): bcform=BaseCaseForm(request.POST) bcform.save() basecasename = bcform.cleaned_data[\'post\'] version = bcform.cleaned_data[\'post\'] default = bcform.cleaned_data[\'post\'] bcform = BaseCaseForm() return redirect(\'index.html\') args = {\'bcform\':bcform, \'basecasename\': basecasename, \'version\': version, \'default\' :default} return render(request, \'index.html\', args) index.html

    <pre class=""lang-html" prettyprint-override="">```

    <html>
    <head>
    <title>S&FP</title>
    </head>
    <body>
    <h1>Forms</h1>
    {% csrf_token %}
    {{ bcform }}
    <input type="submit" value="add">
    </body>
    </html>

    and i think that this is important too urls.py

    <pre class="\"lang-py" prettyprint-override="">``` from django.conf.urls import url from . import views urlpatterns = [ url(r\'^$\', views.index), url(r\'^$\', views.addbc), ] I was expecting the form fields to be generated automatically but they don\\'t appear! 回答1: ==== 0 You can try CreateView which will create forms for your model. Find more about it in the docs In your case, create a view like this: views.py

    class BaseCaseCreate(CreateView):
    model = BaseCase
    template_name = 'index.html'
    success_url = reverse_lazy('app:home')
    fields = ('base_case_name','version','default')

    index.html

    <html>
    <head>
    <title>S&FP</title>
    </head>
    <body>
    <h1>Forms</h1>
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="add">
    </body>

    I hope this helps. share|improve this answer edited 2 days ago answered Nov 7 at 11:53 Srijwal RSrijwal R 1644 bronze badges - the application doesn't render any html file should use this with the function already defined in views.py – Mehdi Selbi Nov 7 at 13:19 - Create the function inside views and mention that in urls.py . Already did it? – Srijwal R Nov 8 at 6:38 - Hi there, sorry for responding this late, I didn't know which function you are talking about? – Mehdi Selbi 2 days ago - Have you tried the above code in your views.py? – Srijwal R 2 days ago - it didn't work. should i define addbc() inside this class? – Mehdi Selbi 2 days ago | show 3 more comments

    read more
  • You can try CreateView which will create forms for your model. Find more about it in the docs

    In your case, create a view like this:

    views.py

    class BaseCaseCreate(CreateView): model = BaseCase template_name = 'index.html' success_url = reverse_lazy('app:home') fields = ('base_case_name','version','default')

    index.html

    <html> <head> <title>S&FP</title> </head> <body> <h1>Forms</h1> {% csrf_token %} {{ form }} <input type="submit" value="add"> </body>

    I hope this helps.

    read more
  • I\'m creating Django forms using model forms because u I wanted the forms to be created automatically, but when I created this code the forms do not appear in the index.html page

    models.py

    <pre class="\"lang-py" prettyprint-override="">``` from django.db import models class BaseCase(models.Model): base_case_name = models.CharField(primary_key=True, max_length=255) version = models.TextField(blank=True, null=True) default = models.TextField(blank=True, null=True) # This field type is a guess. class Meta: managed = False db_table = \'base_case\' forms.py

    <pre class=""lang-py" prettyprint-override="">```
    from django import forms
    from SFP.models import *

    class BaseCaseForm(forms.ModelForm):
    class Meta :
    model = BaseCase
    fields='all'

    views.py

    <pre class="\"lang-py" prettyprint-override="">``` from django.shortcuts import render,redirect from .models import * from .forms import * def addbc(self, request): bcform=BaseCaseForm(request.POST) bcform.save() basecasename = bcform.cleaned_data[\'post\'] version = bcform.cleaned_data[\'post\'] default = bcform.cleaned_data[\'post\'] bcform = BaseCaseForm() return redirect(\'index.html\') args = {\'bcform\':bcform, \'basecasename\': basecasename, \'version\': version, \'default\' :default} return render(request, \'index.html\', args) index.html

    <pre class=""lang-html" prettyprint-override="">```

    <html>
    <head>
    <title>S&FP</title>
    </head>
    <body>
    <h1>Forms</h1>
    {% csrf_token %}
    {{ bcform }}
    <input type="submit" value="add">
    </body>
    </html>

    and i think that this is important too urls.py

    <pre class="\"lang-py" prettyprint-override="">``` from django.conf.urls import url from . import views urlpatterns = [ url(r\'^$\', views.index), url(r\'^$\', views.addbc), ] I was expecting the form fields to be generated automatically but they don\\'t appear!

    read more

最新主题

181
Online

336
Users

219.5k
Topics

370.1k
Posts

在线用户

推荐阅读

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