-1

Thats my register controller here i want user to upload his profile picture and after that when he logs in it must display.

public ActionResult Register(REGISTRATION registration)
{

    var email = registration.Email;
    var username = registration.UserName;
    var pass = registration.Password;
    var confirmpass = registration.ConfirmPassword;
    var mobile = registration.MobileNumber;
    byte image = Convert.ToByte(registration.Image);
    OracleConnection connection = new OracleConnection();
    connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionDHC"].ToString();
    connection.Open();
    OracleCommand command = connection.CreateCommand();
    string query = "INSERT INTO REGISTRATION (EMAIL,USERNAME,PASSWORD,CONFIRMPASSWORD,MOBILENUMBER,IMAGE) VALUES('" + email + "','" + username + "','" + pass + "','" + confirmpass + "','" + mobile + "','" + image + "')";
    command.CommandText = query;

    if (MailExists(registration))
    {
        return View("MailExists");
    }

    command.ExecuteNonQuery();
    return View("Congratulation");
}

what i have to add that it must save image path.??

faraz
  • 57
  • 1
  • 4
  • 12
  • The question you'd asked is a little bit unclear... Can you be more explicit please? I can say that your image is saved in Registration table among with username/email/etc fields in base64 format (it is OK for small image profiles). I cannot understand OK what you want *what i have to add that it must save image path.??* you want to save the path .. ? – mihai Sep 18 '17 at 10:47
  • Possible duplicate of [Store image in database and retrieve it](https://stackoverflow.com/questions/9069742/store-image-in-database-and-retrieve-it) – martennis Sep 18 '17 at 11:39
  • yes the image is being stored but i was in need of the path........ – faraz Sep 19 '17 at 05:25

1 Answers1

0

Are you take image input from fileupload then also store file type (.jpeg / .jpg /.png ) which will help you to display image on View.

Add File input in your View to take Photo

@using (Html.BeginForm("Index", "Default4", FormMethod.Post, new { enctype = "multipart/form-data" }))

{

<input id="File1" name="File1" type="file" />
<input id="Submit1" type="submit" value="submit" />

}

Get Posted File in Controller

    public ActionResult Register(REGISTRATION registration)
    {

        var email = registration.Email;
        var username = registration.UserName;
        var pass = registration.Password;
        var confirmpass = registration.ConfirmPassword;
        var mobile = registration.MobileNumber;
        string base64 = string.Empty;

        // Getting Posted File

        if (Request.Files.Count > 0)
        {
            HttpPostedFileBase file = Request.Files[0];
            if (file.ContentLength > 0)
            {
                Stream stream = file.InputStream;
                byte[] byteArray = ReadFully(stream);
                base64 = Convert.ToBase64String(byteArray);
            }
        }

        byte image = Convert.ToByte(registration.Image);
        OracleConnection connection = new OracleConnection();
        connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionDHC"].ToString();
        connection.Open();
        OracleCommand command = connection.CreateCommand();
        string query = @"INSERT INTO REGISTRATION (EMAIL,USERNAME,PASSWORD,CONFIRMPASSWORD,MOBILENUMBER,IMAGE) VALUES
             ('" + email + "','" + username + "','" + pass + "','" + confirmpass + "','" + mobile + "','" + base64 + "')";
        command.CommandText = query;

        if (MailExists(registration))
        {
            return View("MailExists");
        }

        command.ExecuteNonQuery();
        return View("Congratulation");
    }

To Display on View

@model Application.Models.Registration

@if (registration.Image != null)
{
    var base64 = registration.Image;
    var imgSrc = String.Format("data:image/jpeg;base64,{0}", base64);
    <img src="@imgSrc" alt="Image" width="256px" height="64px" />
}
Saineshwar Bageri - MVP
  • 3,851
  • 7
  • 41
  • 47