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 |



最新内容

  • I am new to android development, I am basically trying to create a very basic app which will enable the user to switch on their wifi.

    My XML file:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" android:gravity="top" android:text="@string/welcome" android:textSize="25sp" /> <Button android:id="@+id/wifi_on" android:layout_width="150dp" android:layout_height="60dp" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="112dp" android:text="@string/wifi_button" />

    `

    My Java file:

    package com.example.dosomething;

    import android.os.Bundle;``import android.app.Activity;``import android.view.Menu;``import android.view.View;``import android.view.View.OnClickListener;``import android.widget.Button;

    public class MainActivity extends Activity implements OnClickListener {

    protected Button wifi_on; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub } public Button getWifiOn(){ return wifi_on; } public void setWifiOn(Button on){ wifi_on = on; }

    }`

    My question is how can I refer to the button inside the XML file onto the java file to further tell it that if the button is clicked on it should turn on the wifi. Thanks.

    There are several ways of doing this.

    What you can do is add this line to your <Button tag in xml

    android:onClick="setWifiOn"

    then change the parameter of that function to

    public void getWifiOn(View v){ return wifi_on; }

    with this you don't need the onClick or any listeners

    2.You can do something similar if you want all Buttons to share the same function then give them all the function name like

    android:onClick="someFunction"

    then in Java do something like

    public void someFunction(View v) { Button btn = (Button)v; switch (v.getId()) { case (R.id.wifi_on: setWifiOn(btn); break; case (R.id.differentBtnId): // do some other things break; } } }

    3.Less attractive in many situations, IMHO

    public class MainActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button wifiBtn = (Button) findViewById(R.id.wifi_on); wifiBtn.setOnClickListener(this); // your onClick below will be called then you will still have to check the id of the Button // multiple buttons can set the same listener and use a switch like above } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub }

    Note number 3 is the only one in which you need implements OnClickListener

    Button Docs

    I left out the other way because I think its the ugliest if you have more than one Button

    来源:https://stackoverflow.com/questions/17958552/android-development-linking-xml-button-to-java

    read more
  • Guys Please Help Me in The Morning it Was Working Fine Suddenly While i Was Running Test, it asked Me a Popup to Allow Access. Why i Dont Know. First i Cancelled That Pop up after again i Run The Test and Gave Access. From That Time Chrome Driver is Not Opening.

    img

    Error Stack Trace :

    java.lang.IllegalStateException: The driver executable does not exist: E:\Workspace\xxxxxxxxx\‪‪C:\Users\xxxxx\Downloads\chromedriver.exe at com.google.common.base.Preconditions.checkState(Preconditions.java:199) at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:121) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:116) at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1) at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296) at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116) at Lead.FinalTest.beforeTest(FinalTest.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138) at org.testng.TestRunner.beforeRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:616) at org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at org.testng.SuiteRunner.run(SuiteRunner.java:261) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215) at org.testng.TestNG.runSuitesLocally(TestNG.java:1140) at org.testng.TestNG.run(TestNG.java:1048) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

    This error message...

    java.lang.IllegalStateException: The driver executable does not exist: E:\Workspace\xxxxxxxxx\‪‪C:\Users\xxxxx\Downloads\chromedriver.exe

    ...implies that the JVM was unable to find the ChromeDriver in the effective location as per the configuration mentioned within your code.

    As per your code trial, you have mentioned the location of the ChromeDriver as:

    C:\Users\xxxxx\Downloads\chromedriver.exe

    But when your testcase gets executed the effective location is considered as:

    E:\Workspace\xxxxxxxxx\‪‪C:\Users\xxxxx\Downloads\chromedriver.exe

    So, there are some configuration within your code block or IDE configuration or OS PATH variable which modifies the absolute path of the ChromeDriver binary. Addressing this issue will solve your problem.

    As error says, there is no executable driver exists in the path you have written.

    Make sure, that driver still exists in the path. Try to replace driver(if exists) in another folder and change path in the code Reinstall driver and set new path in the code

    PS: this is not a valid location:

    E:\Workspace\xxxxxxxxx\‪‪C:\Users\xxxxx\Downloads\chromedriver.exe

    put chrome driver at
    C:\Users\xxxxx\Downloads\chromedriver.exe
    and set same path in code System.setProperty("webdriver.chrome.driver","C:\Users\xxxxx\Downloads\chromedriver.exe");

    This problem problem caused by special character in your path - f.e. some folder name contains 'ą' or 'ś' or space

    来源:https://stackoverflow.com/questions/51607304/java-lang-illegalstateexception-the-driver-executable-does-not-exist-while-try

    read more
  • Taking my initial steps towards Angular 2.0.

    First thing is to set up the right environment for the development.

    My index.html

    <html> <head> <title>Welcome to Angular 2.0</title> <!--css--> <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /> </head> <body> <div class="container"> <h1> Hello Angular 2 </h1> <my-app> Loading app component....<my-app> </div> <!--js--> <!-- Polyfills for older browsers --> <script src="https://unpkg.com/core-js/client/shim.min.js"></script> <script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script> <script src="https://unpkg.com/reflect-metadata@0.1.8"></script> <script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"> </script> <script> window.autoBootstrap = true; </script> <script src="https://cdn.rawgit.com/angular/angular.io/b3c65a9/public/docs/_examples/_boilerplate/systemjs.config.web.js"></script> <script> System.import('app').catch(function (e) { console.log(e); }); </script> <!--js--> </body> </html>

    I have copied the content from https://angular.io/docs/ts/latest/guide/setup.html for the following files:-

    app.component.ts app.module.ts main.ts

    NPM is installed & running.

    NodeJS

    But in order to get the node_modules folders with the required dependencies for the app to run. What commands do I need to run.

    What are the commands that I need to run to set up the Angular 2 environment?

    Please note I am new to NPM. Thanks.

    npm install in the root project (it will create your node_modules from the dependencies mentioned in package.json file, should have those files as a start)

    You have angular-cli wich is a good starter point. It will set you everything up according to best practices.

    Simple way to set up your first Angular 2 Application.

    Download the zip version here.firstAngular2App Extract it to your destination folder. Assume D: Open your command prompt(ensure that all required softwares are installed). Navigate it to the folder. Use the command cd D:\firstAngular2App Executre npm install Once it is done use npm start

    This way you have your first Angular2 appliction up and running.

    If you are already using Visual Studio Code as an IDE, you may want to explore how they recommend setting up Angular. I personally found their installation guide painless and feature-rich. I have never walked through an official Angular installation before due to being drenched in jQuery projects. This guide was simple enough to give me a good beginners understanding.

    来源:https://stackoverflow.com/questions/41569920/how-to-setup-the-environment-for-angular-2-0

    read more

最新主题

311
Online

338
Users

226.0k
Topics

379.4k
Posts

在线用户

推荐阅读

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