gpt4 book ai didi

c# - 如何在数据库中保存图片路径

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:23 25 4
gpt4 key购买 nike

我想将图像路径保存在我的数据库中。图像已正确保存在文件夹中,我不知道如何在上传时将图像路径保存在数据库中。请帮我解决一下。

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());
}

文件上传.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());
}
}

关于c# - 如何在数据库中保存图片路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20825067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com