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 |



最新内容

  • Right now I have two types:

    type Rating = (String, Int) type Film = (String, String, Int, [Rating])

    I have a file that has this data in it:

    "Blade Runner" "Ridley Scott" 1982 ("Amy",5), ("Bill",8), ("Ian",7), ("Kevin",9), ("Emma",4), ("Sam",7), ("Megan",4) "The Fly" "David Cronenberg" 1986 ("Megan",4), ("Fred",7), ("Chris",5), ("Ian",0), ("Amy",6)

    How can I look through then file storing all of the entries into something like FilmDatabase = [Film] ?

    Greg Bacon

    Haskell provides a unique way of sketching out your approach. Begin with what you know

    module Main where type Rating = (String, Int) type Film = (String, String, Int, [Rating]) main :: IO () main = do films <- readFilms "ratings.dat" print films

    Attempting to load this program into ghci will produce

    films.hs:8:12: Not in scope: `readFilms'

    It needs to know what readFilms is, so add just enough code to keep moving.

    readFilms = undefined

    It is a function that should do something related to Film data. Reload this code (with the :reload command or :r for short) to get

    films.hs:9:3: Ambiguous type variable `a0' in the constraint: (Show a0) arising from the use of `print' ...

    The type of print is

    Prelude> :t print print :: Show a => a -> IO ()

    In other words, print takes a single argument that, informally, knows how to show itself (that is, convert its contents to a string) and creates an I/O action that when executed outputs that string. It’s more-or-less how you expect print to work:

    Prelude> print 3 3 Prelude> print "hi" "hi"

    We know that we want to print the Film data from the file, but, although good, ghc can’t read our minds. But after adding a type hint

    readFilms :: FilePath -> Film readFilms = undefined

    we get a new error.

    films.hs:8:12: Couldn't match expected type `IO t0' with actual type `(String, String, Int, [Rating])' Expected type: IO t0 Actual type: Film In the return type of a call of `readFilms' In a stmt of a 'do' expression: films <- readFilms "ratings.dat"

    The error tells you that the compiler is confused about your story. You said readFilms should give it back a Film, but the way you called it in main, the computer should have to first perform some I/O and then give back Film data.

    In Haskell, this is the difference between a pure string, say "JamieB", and a side effect, say reading your input from the keyboard after prompting you to input your Stack Overflow username.

    So now we know we can sketch readFilms as

    readFilms :: FilePath -> IO Film readFilms = undefined

    and the code compiles! (But we can’t yet run it.)

    To dig down another layer, pretend that the name of a single movie is the only data in ratings.dat and put placeholders everywhere else to keep the typechecker happy.

    readFilms :: FilePath -> IO Film readFilms path = do alldata <- readFile path return (alldata, "", 0, [])

    This version compiles, and you can even run it by entering main at the ghci prompt.

    In dave4420’s answer are great hints about other functions to use. Think of the method above as putting together a jigsaw puzzle where the individual pieces are functions. For your program to be correct, all the types must fit together. You can make progress toward your final working program by taking little babysteps as above, and the typechecker will let you know if you have a mistake in your sketch.

    Things to figure out:

    How do you convert the whole blob of input to individual lines? How do you figure out whether the line your program is examining is a title, a director, and so on? How do you convert the year in your file (a String) to an Int to cooperate with your definition of Film? How do you skip blank or empty lines? How do you make readFilms accumulate and return a list of Film data?

    Is this homework?

    You might find these functions useful:

    readFile :: FilePath -> IO String lines :: String -> [String] break :: (a -> Bool) -> [a] -> ([a], [a]) dropWhile :: (a -> Bool) -> [a] -> [a] null :: [a] -> Bool read :: Read a => String -> a

    Remember that String is the same as [Char].

    Some clues:

    dropWhile null will get rid of empty lines from the start of a list break null will split a list into the leading run of non-empty lines, and the rest of the list

    Haskell has a great way of using the types to find the right function. For instance: In Gregs answer, he wants you to figure out (among other things) how to convert the year of the film from a String to an Int. Well, you need a function. What should be the type of that function? It takes a String and returns an Int, so the type should be String -> Int. Once you have that, go to Hoogle and enter that type. This will give you a list of functions with similar types. The function you need actually has a slightly different type - Read a => String -> a - so it is a bit down the list, but guessing a type and then scanning the resulting list is often a very useful strategy.

    来源:https://stackoverflow.com/questions/10175251/reading-file-into-types-haskell

    read more
  • 自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式。

    一、准备工作 1.扫雷游戏

    我是win10,没有默认的扫雷,所以去扫雷网下载

    http://www.saolei.net/BBS/

    2.python 3

    我的版本是 python 3.6.1

    3.python的第三方库

    win32api,win32gui,win32con,Pillow,numpy,opencv
    可通过 pip install --upgrade SomePackage 来进行安装
    注意:有的版本是下载pywin32,但是有的要把pywin32升级到最高并自动下载了pypiwin32,具体情况每个python版本可能都略有不同

    我给出我的第三方库和版本仅供参考

    二、关键代码组成

    找到游戏窗口与坐标

    <pre class="has"> ```python #扫雷游戏窗口 #Python学习交流群:973783996 class_name = "TMain" title_name = "Minesweeper Arbiter " hwnd = win32gui.FindWindow(class_name, title_name) #窗口坐标 left = 0 top = 0 right = 0 bottom = 0 if hwnd: print("找到窗口") left, top, right, bottom = win32gui.GetWindowRect(hwnd) #win32gui.SetForegroundWindow(hwnd) print("窗口坐标:") print(str(left)+' '+str(right)+' '+str(top)+' '+str(bottom)) else: print("未找到窗口") **锁定并抓取雷区图像**

    <pre class="has">

    #锁定雷区坐标 #去除周围功能按钮以及多余的界面 #具体的像素值是通过QQ的截图来判断的 #小编整理一套Python资料和PDF,有需要Python学习资料可以加学习群:631441315 ,反正闲着也是闲着呢,不如学点东西啦~~ left += 15 top += 101 right -= 15 bottom -= 42 #抓取雷区图像 rect = (left, top, right, bottom) img = ImageGrab.grab().crop(rect) **各图像的RGBA值**

    <pre class="has">

    #数字1-8 周围雷数 #0 未被打开 #ed 被打开 空白 #hongqi 红旗 #boom 普通雷 #boom_red 踩中的雷 rgba_ed = [(225, (192, 192, 192)), (31, (128, 128, 128))] rgba_hongqi = [(54, (255, 255, 255)), (17, (255, 0, 0)), (109, (192, 192, 192)), (54, (128, 128, 128)), (22, (0, 0, 0))] rgba_0 = [(54, (255, 255, 255)), (148, (192, 192, 192)), (54, (128, 128, 128))] rgba_1 = [(185, (192, 192, 192)), (31, (128, 128, 128)), (40, (0, 0, 255))] rgba_2 = [(160, (192, 192, 192)), (31, (128, 128, 128)), (65, (0, 128, 0))] rgba_3 = [(62, (255, 0, 0)), (163, (192, 192, 192)), (31, (128, 128, 128))] rgba_4 = [(169, (192, 192, 192)), (31, (128, 128, 128)), (56, (0, 0, 128))] rgba_5 = [(70, (128, 0, 0)), (155, (192, 192, 192)), (31, (128, 128, 128))] rgba_6 = [(153, (192, 192, 192)), (31, (128, 128, 128)), (72, (0, 128, 128))] rgba_8 = [(149, (192, 192, 192)), (107, (128, 128, 128))] rgba_boom = [(4, (255, 255, 255)), (144, (192, 192, 192)), (31, (128, 128, 128)), (77, (0, 0, 0))] rgba_boom_red = [(4, (255, 255, 255)), (144, (255, 0, 0)), (31, (128, 128, 128)), (77, (0, 0, 0))] **扫描雷区图像保存至一个二维数组map**

    <pre class="has">

    #扫描雷区图像 def showmap(): img = ImageGrab.grab().crop(rect) for y in range(blocks_y): for x in range(blocks_x): this_image = img.crop((x * block_width, y * block_height, (x + 1) * block_width, (y + 1) * block_height)) if this_image.getcolors() == rgba_0: map[y][x] = 0 elif this_image.getcolors() == rgba_1: map[y][x] = 1 elif this_image.getcolors() == rgba_2: map[y][x] = 2 elif this_image.getcolors() == rgba_3: map[y][x] = 3 elif this_image.getcolors() == rgba_4: map[y][x] = 4 elif this_image.getcolors() == rgba_5: map[y][x] = 5 elif this_image.getcolors() == rgba_6: map[y][x] = 6 elif this_image.getcolors() == rgba_8: map[y][x] = 8 elif this_image.getcolors() == rgba_ed: map[y][x] = -1 elif this_image.getcolors() == rgba_hongqi: map[y][x] = -4 elif this_image.getcolors() == rgba_boom or this_image.getcolors() == rgba_boom_red: global gameover gameover = 1 break #sys.exit(0) else: print("无法识别图像") print("坐标") print((y,x)) print("颜色") print(this_image.getcolors()) sys.exit(0) #print(map) **扫雷算法** -------- 这里我采用的最基础的算法 1.首先点出一个点 2.扫描所有数字,如果周围空白+插旗==数字,则空白均有雷,右键点击空白插旗 3.扫描所有数字,如果周围插旗==数字,则空白均没有雷,左键点击空白 4.循环2、3,如果没有符合条件的,则随机点击一个白块

    <pre class="has">

    #插旗 def banner(): showmap() for y in range(blocks_y): for x in range(blocks_x): if 1 <= map[y][x] and map[y][x] <= 5: boom_number = map[y][x] block_white = 0 block_qi = 0 for yy in range(y-1,y+2): for xx in range(x-1,x+2): if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x: if not (yy == y and xx == x):if map[yy][xx] == 0: block_white += 1 elif map[yy][xx] == -4: block_qi += 1if boom_number == block_white + block_qi:for yy in range(y - 1, y + 2): for xx in range(x - 1, x + 2): if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x: if not (yy == y and xx == x): if map[yy][xx] == 0: win32api.SetCursorPos([left+xx*block_width, top+yy*block_height]) win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) showmap() #点击白块 def dig(): showmap() iscluck = 0 for y in range(blocks_y): for x in range(blocks_x): if 1 <= map[y][x] and map[y][x] <= 5: boom_number = map[y][x] block_white = 0 block_qi = 0 for yy in range(y - 1, y + 2): for xx in range(x - 1, x + 2): if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x: if not (yy == y and xx == x): if map[yy][xx] == 0: block_white += 1 elif map[yy][xx] == -4: block_qi += 1if boom_number == block_qi and block_white > 0:for yy in range(y - 1, y + 2): for xx in range(x - 1, x + 2): if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x: if not(yy == y and xx == x): if map[yy][xx] == 0: win32api.SetCursorPos([left + xx * block_width, top + yy * block_height]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) iscluck = 1 if iscluck == 0: luck() #随机点击 def luck(): fl = 1 while(fl): random_x = random.randint(0, blocks_x - 1) random_y = random.randint(0, blocks_y - 1) if(map[random_y][random_x] == 0): win32api.SetCursorPos([left + random_x * block_width, top + random_y * block_height]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) fl = 0 def gogo(): win32api.SetCursorPos([left, top]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) showmap() global gameover while(1): if(gameover == 0): banner() banner() dig() else: gameover = 0 win32api.keybd_event(113, 0, 0, 0) win32api.SetCursorPos([left, top]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) showmap() **这个算法在初级和中级通过率都不错,但是在高级成功率惨不忍睹,主要是没有考虑逻辑组合以及白块是雷的概率问题,可以对这两个点进行改进,提高成功率** 来源:`https://blog.csdn.net/ooowwq/article/details/99697545`

    read more
  • _

    How do I add a string with auto-increment value in SQL Server?

    create table date (sno int 'emp_'+ identity(1,1))

    I need following as output

    emp_1 emp_2 Define your table with a normal INT IDENTITY column Add a computed column that merges string and ID together:

    Something like this:

    CREATE TABLE dbo.YourTable (ID INT IDENTITY(1,1), EmpID AS 'emp_' + CAST(ID AS VARCHAR(10)) PERSISTED, ...... )

    This way, your ID column is a normal IDENTITY column, and EmpID will contain emp_1, emp_2, emp_3, ......, emp_42, ...

    By making it a persisted computed column, the data is actually stored on disk and doesn't need to be re-calculated every time you access the table. Also, when persisted, you can easily put an index on that computed column, too

    You can't use any string as autoincrement

    Lets think you have a table

    |- id -|- name -| |- 1 -|- Utku -| |- 2 -|- Gopi -|

    Lets select them as emp_1,emp_2

    SELECT CONCAT("emp_",id) AS emp_id,name FROM table

    Result:

    |- emp_id -|- name -| |- emp_1 -|- Utku -| |- emp_2 -|- Gopi -|

    Try using computed column MSDN

    CREATE TABLE Yourtablename ( ID int IDENTITY (1,1) NOT NULL InvoiceID AS 'INV'+ right('000000' cast(ID as varchar(20),7) );

    来源:https://stackoverflow.com/questions/8170950/how-do-i-add-string-with-auto-increment-value-in-sql-server

    read more

最新主题

309
Online

338
Users

226.0k
Topics

379.4k
Posts

在线用户

推荐阅读

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