Click here to Skip to main content
15,906,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys


OK Quick Overview

Class Employee like Below + some extra supporting classes
But the focus of my problem is on
public virtual byte[] EmployeePhoto {get; set;}
part

First I was looking to just Upload whole image to DB and later retrieve it back to view from DB but don't know why its look hard for me like I'm new to this that's why i Chose other way

Upload File to folder on the server and path + file name as a string to DB don't know why looks easier for me.

C#
public class Employee
    {
        [Key]
        public virtual int EmployeeID { get; set; }

        [ForeignKey("TitleID")]
        public virtual Title Title { get; set; }

        [Display(Name = "Title")]
        public virtual int TitleID { get; set; }



        [Display(Name = "First Name")]
        [Required]
        [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
        public virtual string FirstName { get; set; }
        [Display(Name = "Last Name")]
        [Required]
        [StringLength(50, ErrorMessage = "Last name cannot be longer than 50 characters.")]
        public virtual string LastName { get; set; }

        [Display(Name = "Full Name")]
        public virtual string FullName
        {
            get
            {
                return LastName + ", " + FirstName;
            }
        }

        [Display(Name = "Date of Birth")]
        [DataType(DataType.Date)]
        [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public virtual DateTime? DOB { get; set; }

        [ForeignKey("GenderID")]
        public virtual Gender Gender { get; set; }

        [Display(Name = "Gender")]
        public virtual int GenderID { get; set; }

        [Display(Name = "Adress Line 1")]
        public virtual string AddressLine1 { get; set; }
        [Display(Name = "Adress Line 2")]
        public virtual string AddressLine2 { get; set; }

        public virtual string City { get; set; }

        [Display(Name = "Post Code")]
        public virtual string PostCode { get; set; }

        [Display(Name = "Email")]
        public virtual string EmailAddress { get; set; }

        [Display(Name = "Phone Nr")]
        public virtual int Phone { get; set; }

        [Display(Name = "Employment Nr")]
        public virtual int EmploymentNr { get; set; }

        [Display(Name = "Start Date")]
        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(NullDisplayText = "" , DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public virtual DateTime? HireDate { get; set; }

        [Display(Name = "Holiday Total Allowed")]
        public virtual int HolidayTotal { get; set; }

        [Display(Name = "Sick Leave Total Allowed")]
        public virtual int SickLeaveTotal { get; set; }

        [ForeignKey("ShiftID")]
        public virtual Shift Shift { get; set; }

        [Required]
        [Display(Name = "Shift")]
        public virtual int ShiftID { get; set; }

        public virtual byte[] EmployeePhoto { get; set; }



    }

    public class Title
    {
        public virtual int TitleID { get; set; }
        [Column("Title")]
        [Display(Name = "Title")]
        [Required]
        public virtual string Name { get; set; }

    }
    public class Gender
    {
        public virtual int GenderID { get; set; }
        [Column("Gender")]
        [Display(Name = "Gender")]
        [Required]
        public virtual string Name { get; set; }
    }
    public class Shift
    {
        public virtual int ShiftID { get; set; }
        [Column("Shift")]
        [Display(Name = "Shift")]
        [Required]
        public virtual string Name { get; set; }
        [Display(Name = "Start Time")]
        [Required]
        [DataType(DataType.Time)]
        [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:hh:mm}", ApplyFormatInEditMode = true)]
        public virtual DateTime StartTime { get; set; }
        [Display(Name = "End Time")]
        [Required]
        [DataType(DataType.Time)]
        [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:hh:mm}", ApplyFormatInEditMode = true)]
        public virtual DateTime EndTime { get; set; }


    }
}



I think I need to change
public virtual byte[] EmployeePhoto {get; set;}

to
public virtual string Photo {get; set;}


Then Edit the View Model for Create Employee, take part of Model.Photo

And add
XML
<form action="Categories/Upload" enctype="multipart/form-data" method="post">
    <input type="file" name="Image">
    <input type="submit" value"Save">
</form>



Then I need to edit my Controller Adding


Something like

[HttpPost]
        public ActionResult Upload(string ActionName)
        {
            var path = Server.MapPath("~/App_Data/Files");
            foreach (string item in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[item];
                if (file.ContentLength == 0)
                {
                    //Repeated upload file be skipped .
                    continue;
                }
                string savedFileName = Path.Combine(path, Path.GetFileName(file.FileName));
                file.SaveAs(savedFileName);
            }
            return RedirectToAction(ActionName);
        }



but I don't Know how to Perform saving of the file path as a string to my DB Photo Column

that's the issue and that's the problem i need some help


also i have a costume Html helper to retrieve the whole thing later from


C#
public static class CustomHtmlHelper
    {
        public static IHtmlString Image(this HtmlHelper helper, string src, string alt)
        {
            // Build <img> tag
            TagBuilder tb = new TagBuilder("img");
            // Add "src" attribute
            tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
            // Add "alt" attribute
            tb.Attributes.Add("alt", alt);
            // return MvcHtmlString. This class implements IHtmlString
            // interface. IHtmlStrings will not be html encoded.
            return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
        }
    }


result will be

@Html.Image(Model.Photo, Model.AlternateText)


What will return Photo in my detiles view model


I'm also open for other solutions as I'm learning and this may be a wrong approach


Best Regards
Posted
Comments
Prasaad SJ 18-Dec-13 22:32pm    
Hi Sebsco,
couldn't understand your question well. Do you want a suggestion or.. ??. Can you describe what you want in detail.. ??
SebSCO 19-Dec-13 5:06am    
I'm looking for a way to modifies my HttPost Upload Controll to make sure when person upload a image/photo it will add the photo to the folder what it do right now like you see on the code i attach and it will add path + FileName to the DataBase Photo Column i Created

or any other suggestion how i should approach this

the final result i want to achieve is showing Detiles of my Employee + his Photo in the Detiles view Model

1 solution

You have the code to save your image. You are right, you need to change the image property to a string, if you're going to save a path to the image. I also suggest a folder structure like this:

pictures/userid/img file

so that two users can upload images with the same name, but they won't clash, as each user has a folder that is named after the primary key that identifies the user.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900