我想在我的数据库中保存图像路径。图像已妥善保存在文件夹中,我不知道如何在上传数据库时保存图像路径。请帮我解决一下。
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<T> 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
}
答案 0 :(得分:1)
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());
}
}
答案 1 :(得分:0)
试试这个:
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());
}