简体   繁体   中英

How to save image path in database

I want to save the image path in my database. The image is properly saved in a folder and I do not have any idea how to save the image path in the database as it is uploaded. Please help me solve it.

HomeController.cs

ImageEntities db = new ImageEntities();
public ActionResult FileUpload(HttpPostedFileBase file, tbl_Image model)
        {
            if (file != null)
            {
                string pic = System.IO.Path.GetFileName(file.FileName);
                string path = System.IO.Path.Combine(
                                       Server.MapPath("~/images/profile"), pic);

                file.SaveAs(path);

            }

    return View("FileUploaded", db.tbl_Image.ToList());
}

FileUpload.cshtml

 @using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, 
                                new { enctype = "multipart/form-data" }))
    {  
        <label for="file">Upload Image:</label> 
        <input type="file" name="file" id="file" style="width: 100%;" /> 
        <input type="submit" value="Upload" class="submit" /> 
    }



using System;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;

[assembly: EdmSchemaAttribute()]
namespace ImageUploadMvcApplication.Models
{
    #region Contexts

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public partial class ImageEntities : ObjectContext
    {
        #region Constructors

    /// <summary>
    /// Initializes a new ImageEntities object using the connection string found in the 'ImageEntities' section of the application configuration file.
    /// </summary>
    public ImageEntities() : base("name=ImageEntities", "ImageEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new ImageEntities object.
    /// </summary>
    public ImageEntities(string connectionString) : base(connectionString, "ImageEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new ImageEntities object.
    /// </summary>
    public ImageEntities(EntityConnection connection) : base(connection, "ImageEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    #endregion

    #region Partial Methods

    partial void OnContextCreated();

    #endregion

    #region ObjectSet Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<tbl_Image> tbl_Image
    {
        get
        {
            if ((_tbl_Image == null))
            {
                _tbl_Image = base.CreateObjectSet<tbl_Image>("tbl_Image");
            }
            return _tbl_Image;
        }
    }
    private ObjectSet<tbl_Image> _tbl_Image;

    #endregion

    #region AddTo Methods

    /// <summary>
    /// Deprecated Method for adding a new object to the tbl_Image EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddTotbl_Image(tbl_Image tbl_Image)
    {
        base.AddObject("tbl_Image", tbl_Image);
    }

    #endregion

}

#endregion

#region Entities

/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmEntityTypeAttribute(NamespaceName="ImageModel", Name="tbl_Image")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class tbl_Image : EntityObject
{
    #region Factory Method

    /// <summary>
    /// Create a new tbl_Image object.
    /// </summary>
    /// <param name="id">Initial value of the id property.</param>
    public static tbl_Image Createtbl_Image(global::System.Int32 id)
    {
        tbl_Image tbl_Image = new tbl_Image();
        tbl_Image.id = id;
        return tbl_Image;
    }

    #endregion

    #region Primitive Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int32 id
    {
        get
        {
            return _id;
        }
        set
        {
            if (_id != value)
            {
                OnidChanging(value);
                ReportPropertyChanging("id");
                _id = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("id");
                OnidChanged();
            }
        }
    }
    private global::System.Int32 _id;
    partial void OnidChanging(global::System.Int32 value);
    partial void OnidChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String imagepath
    {
        get
        {
            return _imagepath;
        }
        set
        {
            OnimagepathChanging(value);
            ReportPropertyChanging("imagepath");
            _imagepath = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("imagepath");
            OnimagepathChanged();
        }
    }
    private global::System.String _imagepath;
    partial void OnimagepathChanging(global::System.String value);
    partial void OnimagepathChanged();

    #endregion


}

#endregion

}

public ActionResult FileUpload(HttpPostedFileBase file, tbl_Image model)
{
        using(ImageEntities db = new ImageEntities()){
        if (file != null)
        {
            string pic = System.IO.Path.GetFileName(file.FileName);
            string path = System.IO.Path.Combine(
                                   Server.MapPath("~/images/profile"), pic);
            file.SaveAs(path);
            tbl_Image img=new tbl_Image();
            img.imagepath=path;
            db.tbl_Images.Add(img);
            db.SaveChanges();
        }

        return View("FileUploaded", db.tbl_Image.ToList());
        }
}

Try this:

    ImageEntities db = new ImageEntities();
    public ActionResult FileUpload(HttpPostedFileBase file, tbl_Image model)
    {
            if (file != null)
            {
                string pic = System.IO.Path.GetFileName(file.FileName);
                string path = System.IO.Path.Combine(
                                       Server.MapPath("~/images/profile"), pic);
                file.SaveAs(path);
                db.AddTotbl_Image(new tbl_Image(){imagepath="~/images/profile/"+pic});
                db.SaveChanges();
            }

            return View("FileUploaded", db.tbl_Image.ToList());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM