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.

    回答1:

    0

    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!

    share|improve this answer

    answered 11 hours ago

    Mizu HaqueMizu Haque 133 bronze badges

    add a comment |



最新内容

  • 题目链接:https://leetcode-cn.com/problems/isomorphic-strings/

    题目描述:

    给定两个字符串 s 和 t,判断它们是否是同构的。

    如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。

    所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。

    示例:

    示例 1:

    输入: s = "egg", t = "add" 输出: true

    示例 2:

    输入: s = "foo", t = "bar" 输出: false

    示例 3:

    输入: s = "paper", t = "title" 输出: true

    说明:

    你可以假设 s 和 t 具有相同的长度。

    思路:

    解释题意:s中字母对应t中一个字母,位置也是对应的。但是,不能映射两个,比如aa , ab。a对应a,又对应b。

    思路一:

    先把s,t各自分组,什么意思呢?就是连续出现相同的字符有一组,如何同一位置,连续相同的个数都不一样说明,不能映射,再哈希判断字符是否对应。

    思路二:用两个字典

    就是看映射的字母位置是相同的

    直接看代码理解吧!

    如有不清楚地方,欢迎留言!

    代码:

    思路一:

    class Solution: def isIsomorphic(self, s: str, t: str) -> bool: lookup = {} for a, b in zip(itertools.groupby(s), itertools.groupby(t)): #print(a, b) a1, a2 = a[0], a[1] b1, b2 = b[0], b[1] if len(list(a2)) != len(list(b2)): return False if a1 not in lookup: lookup[a1] = b1 else: if lookup[a1] != b1: return False return True

    思路二:

    class Solution: def isIsomorphic(self, s: str, t: str) -> bool: from collections import defaultdict lookup1 = defaultdict(lambda:-1) lookup2 = defaultdict(lambda:-1) for i in range(len(s)): if lookup1[s[i]] != lookup2[t[i]]: return False lookup1[s[i]] = i lookup2[t[i]] = i return True

    来源:https://www.cnblogs.com/powercai/p/11370298.html

    read more
  • I am using fancybox 2. I have manage to load content in fancybox via AJAX. But it loads the whole page and I am only interested in specific div. In fancybox 1 we could do that by adding filter in ajax. But I am not sure how to filter in fancybox 2.

    How to filter specific div from AJAX loaded page in fancybox 2?

    $(".fancybox").fancybox({ maxWidth : 800, maxHeight : 600, fitToView : false, width : '70%', height : '70%', autoSize : false, closeClick : false, openEffect : 'none', closeEffect : 'none', type : 'ajax', 'ajax' : { dataFilter: function(data) { return $(data).find('#modalArticleContainer')[0]; } });

    Untile ajax it works but loads whole page, when I add filter then it stops working. This is how I did previously in fancybox 1.

    If you are using the latest (https://github.com/fancyapps/fancyBox/zipball/master), then there is a trick to load specific element from the ajax response -

    <a href="mypage.html #my_id" class="fancybox fancybox.ajax">Load ajax</a> $(".fancybox").fancybox();

    discoliam

    I've achieved this using the afterLoad callback to update the content to find your particular element.

    $('.fancybox').fancybox({ afterLoad : function() { this.content = $("<div>").html(this.content).find("#main-content").html(); } });

    So in this example, only content in the #main-content element will be loaded.

    This allows us to just follow the link on mobile, but load the page inside fancybox on desktop without the header and footer etc.

    With fancybox 3 its even more simple:

    <a data-fancybox data-type="ajax" data-src="my_page.com/path/to/ajax/" data-filter="#yourDivId" href="javascript:;">AJAX Content</a>

    Look at the demo

    来源:https://stackoverflow.com/questions/10544021/load-specific-div-in-modal-window-using-fancybox

    read more
  • HTML-popFrame

    弹出层+背景层设置弹出框

    代码

    <span class="token comment">// An highlighted block</span> <span class="token operator"><</span><span class="token operator">!</span><span class="token constant">DOCTYPE</span> <span class="token constant">HTML</span><span class="token operator">></span> <span class="token operator"><</span>html<span class="token operator">></span> <span class="token operator"><</span>head<span class="token operator">></span> <span class="token operator"><</span>meta charset<span class="token operator">=</span><span class="token string">"UTF-8"</span><span class="token operator">></span> <span class="token operator"><</span>title<span class="token operator">></span>popFrame<span class="token operator"><</span><span class="token operator">/</span>title<span class="token operator">></span> <span class="token operator"><</span>script type<span class="token operator">=</span><span class="token string">"text/javascript"</span> src<span class="token operator">=</span><span class="token string">"jquery.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>style<span class="token operator">></span> <span class="token comment">/*背景层*/</span> #popLayer<span class="token punctuation">{</span> display<span class="token punctuation">:</span>none<span class="token punctuation">;</span> background<span class="token operator">-</span>color<span class="token punctuation">:</span>#<span class="token constant">B3B3B3</span><span class="token punctuation">;</span> position<span class="token punctuation">:</span>absolute<span class="token punctuation">;</span> top<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">;</span> right<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">;</span> bottom<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">;</span> left<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">;</span> z<span class="token operator">-</span>index<span class="token punctuation">:</span><span class="token number">10</span><span class="token punctuation">;</span> <span class="token operator">-</span>moz<span class="token operator">-</span>opacity<span class="token punctuation">:</span><span class="token number">0.8</span><span class="token punctuation">;</span> opacity<span class="token punctuation">:</span><span class="token number">.80</span><span class="token punctuation">;</span> filter<span class="token punctuation">:</span><span class="token function">alpha</span><span class="token punctuation">(</span>opacity<span class="token operator">=</span><span class="token number">80</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/*弹出层*/</span> #popBox<span class="token punctuation">{</span> display<span class="token punctuation">:</span>none<span class="token punctuation">;</span> background<span class="token operator">-</span>color<span class="token punctuation">:</span>#<span class="token constant">FFFFFF</span><span class="token punctuation">;</span> z<span class="token operator">-</span>index<span class="token punctuation">:</span><span class="token number">11</span><span class="token punctuation">;</span> width<span class="token punctuation">:</span><span class="token number">200</span>px<span class="token punctuation">;</span> height<span class="token punctuation">:</span><span class="token number">200</span>px<span class="token punctuation">;</span> position<span class="token punctuation">:</span>fixed<span class="token punctuation">;</span> top<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">;</span> right<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">;</span> left<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">;</span> bottom<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">;</span> margin<span class="token punctuation">:</span>auto<span class="token punctuation">;</span> <span class="token punctuation">}</span> #popBox <span class="token punctuation">.</span>close<span class="token punctuation">{</span> text<span class="token operator">-</span>align<span class="token punctuation">:</span>right<span class="token punctuation">;</span> margin<span class="token operator">-</span>right<span class="token punctuation">:</span><span class="token number">5</span>px<span class="token punctuation">;</span> background<span class="token operator">-</span>color<span class="token punctuation">:</span>#<span class="token constant">F8F8F8</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> #popBox <span class="token punctuation">.</span>close a<span class="token punctuation">{</span> text<span class="token operator">-</span>decoration<span class="token punctuation">:</span>none<span class="token punctuation">;</span> color<span class="token punctuation">:</span>#<span class="token number">2</span>D2C3B<span class="token punctuation">;</span> <span class="token punctuation">}</span> #popBox <span class="token punctuation">.</span>content<span class="token punctuation">{</span> text<span class="token operator">-</span>align<span class="token punctuation">:</span>center<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator"><</span><span class="token operator">/</span>style<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>head<span class="token operator">></span> <span class="token operator"><</span>body<span class="token operator">></span> <span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"button"</span> name<span class="token operator">=</span><span class="token string">"popBox"</span> value<span class="token operator">=</span><span class="token string">"popFrame"</span> onclick<span class="token operator">=</span><span class="token string">"popBox()"</span><span class="token operator">></span> <span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"popLayer"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"popBox"</span><span class="token operator">></span> <span class="token operator"><</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"close"</span><span class="token operator">></span> <span class="token operator"><</span>center<span class="token operator">></span>popFrame<span class="token operator"><</span><span class="token operator">/</span>center<span class="token operator">></span> <span class="token operator"><</span>a href<span class="token operator">=</span><span class="token string">"javascript:void(0)"</span> onclick<span class="token operator">=</span><span class="token string">"closeBox()"</span><span class="token operator">></span>close<span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"content"</span> style<span class="token operator">=</span><span class="token string">"padding-top:50px;"</span><span class="token operator">></span>the pop layer<span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>script type<span class="token operator">=</span><span class="token string">"text/javascript"</span><span class="token operator">></span> <span class="token comment">/*点击弹出按钮*/</span> <span class="token keyword">function</span> <span class="token function">popBox</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">var</span> popBox<span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"popBox"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">var</span> popLayer<span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"popLayer"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> popBox<span class="token punctuation">.</span>style<span class="token punctuation">.</span>display<span class="token operator">=</span><span class="token string">"block"</span><span class="token punctuation">;</span> popLayer<span class="token punctuation">.</span>style<span class="token punctuation">.</span>display<span class="token operator">=</span><span class="token string">"block"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">/*点击关闭按钮*/</span> <span class="token keyword">function</span> <span class="token function">closeBox</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">var</span> popBox<span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"popBox"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">var</span> popLayer<span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"popLayer"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> popBox<span class="token punctuation">.</span>style<span class="token punctuation">.</span>display<span class="token operator">=</span><span class="token string">"none"</span><span class="token punctuation">;</span> popLayer<span class="token punctuation">.</span>style<span class="token punctuation">.</span>display<span class="token operator">=</span><span class="token string">"none"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>html<span class="token operator">></span>

    效果代码效果

    来源:https://blog.csdn.net/qq_45538481/article/details/99697638

    read more

最新主题

312
Online

338
Users

226.0k
Topics

379.4k
Posts

在线用户

推荐阅读

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