upload image in server folder and save path in Sqlserver using entity framework mvc4

user3780510

I am trying to save picture in folder and store path in Sqlserver 2008 using entity framework. I need register the user with picture. My code is saving all the data in database except picture and picture path.

My model is 

 {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    public partial class customer
    {

        [Display(Name="Username")]
        public string user_id { get; set; }

        [Display(Name = "Password")]
        public string password { get; set; }

        [Display(Name = "First Name")]
        public string first_name { get; set; }

        [Display(Name = "Last Name")]
        public string last_name { get; set; }

        [Display(Name = "Address")]
        public string address { get; set; }

        [Display(Name = "City")]
        public string city { get; set; }

        [Display(Name = "State")]
        public string state { get; set; }

        [Display(Name = "Zip")]
        public Nullable<int> zip { get; set; }

        [Display(Name = "Country")]
        public string country { get; set; }

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

        [Display(Name = "Phone")]
        public string phone { get; set; }

        [Display(Name = "Picture")]
        public string picture { get; set; }

        [Display(Name = "Registration Date")]
        public Nullable<System.DateTime> reg_date { get; set; }

        [Display(Name = "Status")]
        public string status { get; set; }

        [Display(Name = "Keep me logged in")]
        public bool rememberme { get; set; }
     }
 }

My Controller is 

[HttpPost]
        public ActionResult Register(customer customer, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                        file.SaveAs(HttpContext.Server.MapPath("~/image/") + file.FileName);
                        customer.picture = file.FileName;
                }
                onlinebookstoreEntities1 db = new onlinebookstoreEntities1();
                db.customers.Add(customer);
                db.SaveChanges();
                return RedirectToAction("Index","Home");
            }

            return View(customer);
        }


and my view is 


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


        <div class="editor-label">
            @Html.LabelFor(model => model.first_name)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.first_name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.last_name)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.last_name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.user_id)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.user_id)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.password)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.password)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.address)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.address)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.city)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.city)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.state)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.state)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.zip)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.zip)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.country)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.country)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.email)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.phone)
        </div>
        <div class="txtreg">
            @Html.EditorFor(model => model.phone)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.picture)
        </div>
        <div class="btnreg">
           <input type="file" id="picture" value="Upload Picture" />
        </div>


        <div class="txtreg">
            @Html.CheckBoxFor(model => model.rememberme)   @Html.LabelFor(model => model.rememberme)
        </div>

            <input type="submit" value="Create Account" name="btnsub" />

}
user3804714

If you want to save a picture in the database your property have to look like this:

    [DisplayName("Billede")]
    [MaxLength]
    public byte[] PhotoFile { get; set; }

and not:

       [Display(Name = "Picture")]
    public string picture { get; set; }

You save your picture like this:

 @using (Html.BeginForm("Create", "Photo", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
                @Html.DisplayNameFor(model => model.PhotoFile)
                <input type="file" name="Image" />

And the ActionResult method inside your PhotoController would be

    public ActionResult Create(Photo photo, HttpPostedFileBase image)
    {

and the actual saving of the photo itself

   if (image != null)
            {
                photo.ImageFileName = image.FileName;
                photo.ImageMimeType = image.ContentType;
                photo.PhotoFile = new byte[image.ContentLength];
                image.InputStream.Read(photo.PhotoFile, 0, image.ContentLength);
            }
            context.Add<Photo>(photo);
            context.SaveChanges();

Hope you can connect the dots.

Mohammad

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MVC4 Code First Entity Framework Upload large files to a SQL Server database

From Dev

Upload image to folder in spring mvc

From Dev

Single or multiple DbContext File in mvc4 using Entity framework 6

From Dev

Image upload crop resize save to folder

From Dev

what is the best way to upload images to server? save in database or in server folder?

From Dev

upload image in server folder and save path in Sqlserver using entity framework mvc4

From Dev

How to retrieve image from database without using Entity Framework in ASP.NET MVC4

From Dev

Save and retrieve image (binary) from SQL Server using Entity Framework 6

From Dev

save the path of image being uploaded on server folder in database

From Dev

How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?

From Dev

How to upload Image to a specified folder in server using zk framework

From Dev

PieChart in mvc4 from Entity Framework

From Dev

Unable to Use an HtmlHelper in Razor syntax in MVC4 Intranet App using Entity Framework

From Dev

how to upload image in database save in folder?

From Dev

How can i check size of file using ContentLength before save as in server (MVC4)

From Dev

How to retrieve image from database without using Entity Framework in ASP.NET MVC4

From Dev

How to perform edit function in MVC4 without using entity framework?

From Dev

Multiple Image upload and save path to database

From Dev

upload image using image Path in flask

From Dev

Image upload not placing Image in Server Folder

From Dev

DreamFactory: How to upload an image to the fileserver and save the path of the image in DataBase?

From Dev

upload image to a folder using jqueryajax

From Dev

How to save image from URL in server folder using php or javascript?

From Dev

Upload image on server and add file path inside image tag using quilljs

From Dev

MVC4 image upload and preview

From Dev

Dropdownlist and MVC4 Entity Framework

From Dev

upload image in folder MVC

From Dev

Upload image and save to my folder in php

From Dev

how to upload image in server folder using angularjs and spring

Related Related

  1. 1

    MVC4 Code First Entity Framework Upload large files to a SQL Server database

  2. 2

    Upload image to folder in spring mvc

  3. 3

    Single or multiple DbContext File in mvc4 using Entity framework 6

  4. 4

    Image upload crop resize save to folder

  5. 5

    what is the best way to upload images to server? save in database or in server folder?

  6. 6

    upload image in server folder and save path in Sqlserver using entity framework mvc4

  7. 7

    How to retrieve image from database without using Entity Framework in ASP.NET MVC4

  8. 8

    Save and retrieve image (binary) from SQL Server using Entity Framework 6

  9. 9

    save the path of image being uploaded on server folder in database

  10. 10

    How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?

  11. 11

    How to upload Image to a specified folder in server using zk framework

  12. 12

    PieChart in mvc4 from Entity Framework

  13. 13

    Unable to Use an HtmlHelper in Razor syntax in MVC4 Intranet App using Entity Framework

  14. 14

    how to upload image in database save in folder?

  15. 15

    How can i check size of file using ContentLength before save as in server (MVC4)

  16. 16

    How to retrieve image from database without using Entity Framework in ASP.NET MVC4

  17. 17

    How to perform edit function in MVC4 without using entity framework?

  18. 18

    Multiple Image upload and save path to database

  19. 19

    upload image using image Path in flask

  20. 20

    Image upload not placing Image in Server Folder

  21. 21

    DreamFactory: How to upload an image to the fileserver and save the path of the image in DataBase?

  22. 22

    upload image to a folder using jqueryajax

  23. 23

    How to save image from URL in server folder using php or javascript?

  24. 24

    Upload image on server and add file path inside image tag using quilljs

  25. 25

    MVC4 image upload and preview

  26. 26

    Dropdownlist and MVC4 Entity Framework

  27. 27

    upload image in folder MVC

  28. 28

    Upload image and save to my folder in php

  29. 29

    how to upload image in server folder using angularjs and spring

HotTag

Archive