如何上传个人资料图片并在身份asp.net core 3

我想在身份用户中上传个人资料图片,并在帐户管理中对其进行更新。如果有任何有关asp.net核心示例的帖子,请给我链接。

codyhcm 回答:如何上传个人资料图片并在身份asp.net core 3

我自己使用FileForm方法做到了。首先,您必须在用户类(https://docs.microsoft.com/en-us/aspnet/core/security/authentication/add-user-data?view=aspnetcore-3.0&tabs=visual-studio)中添加字符串属性。

<divs>

,然后按照文档更新“管理/索引”文件。现在,如何创建文件上传系统?我喜欢下面的代码。但是如果我需要更改某些内容以确保安全,请不要忘记提供帮助。

        public string Avatar { get; set; }
PostAsync类之后的

GetUniqueFileName类:

                    if (Input.Avatar != null)
                {
                    string existfile = Path.Combine(hostingEnvironment.WebRootPath,"uploads",user.Avatar);
                    System.IO.File.Delete(existfile);

                }
                var uploads = Path.Combine(hostingEnvironment.WebRootPath,"avatar");
                var filePath = Path.Combine(uploads,fileName);
                this.Image.CopyTo(new FileStream(filePath,FileMode.Create));
                user.Avatar = fileName; // Set the file name

还必须添加IWebHostEnvironment依赖项注入并使用multipart / form-data编码类型更新cshtml表单。不要忘记也要遵守.net文档规则。祝你好运!

本文链接:https://www.f2er.com/3134699.html

大家都在问