为什么图像路径不存储在数据库MVC5中?

发布于 2025-01-22 09:07:23 字数 2090 浏览 0 评论 0 原文

我是MVC的新手,所以我希望就我的问题获得帮助。我正在使用户上传图像。图像路径将存储在数据库中,但图像本身将存储在内容文件夹中。代码工作正常(这意味着没有错误),但是我没有得到想要的结果。感谢您的帮助。提前致谢。

这是控制器代码:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "EmployeeId,EmpName,EmpEmail,Poition,Nationality,Last_W_D,EmpHOD,Password,DepId,SignaturePath,DoctorCategory")] Tbl_Employee tbl_Employee, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if(file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Content/Images/Employees/") + file.FileName);
                    tbl_Employee.SignaturePath = file.FileName;
                }
                db.Tbl_Employee.Add(tbl_Employee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

这是“创建视图”代码:

 @model ClearanceFirst.Models.Tbl_Employee

    @{
        ViewBag.Title = "Create";
    }

    <h2>Create</h2>


    @using (Html.BeginForm("Create", "Tbl_Employee", null, FormMethod.Post, new { enctype = " 
    multipart/form-data" }))
   {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <h4>Tbl_Employee</h4>
            <hr /> 
    <div class="form-group">
                @Html.LabelFor(model => model.SignaturePath, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
               <input id="signaturePath " title ="upload images " type="file" name="file" />
               @Html.ValidationMessageFor(model => model.SignaturePath, "", new { @class = "text-danger" }) 
            </div>
        </div> 

这是“索引视图”代码:

<td>
        @if (!string.IsNullOrWhiteSpace(item.SignaturePath))
        {
            <img width = "70" height = "50"
                 src="~/Content/Images/Employees/@Url.Content(item.SignaturePath)"
                 alt= "Alternate Text"/> }
</td>

I'm new to MVC so I hope to get help regarding my question. I am making users upload images. the image path will store in the database but the image itself will store in the Content folder. The code is working fine (which means there is no error) but I don't get the wanted result. I appreciate your help. Thanks in advance.

Here is the Controller code:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "EmployeeId,EmpName,EmpEmail,Poition,Nationality,Last_W_D,EmpHOD,Password,DepId,SignaturePath,DoctorCategory")] Tbl_Employee tbl_Employee, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if(file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Content/Images/Employees/") + file.FileName);
                    tbl_Employee.SignaturePath = file.FileName;
                }
                db.Tbl_Employee.Add(tbl_Employee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

Here is the "Create view " code:

 @model ClearanceFirst.Models.Tbl_Employee

    @{
        ViewBag.Title = "Create";
    }

    <h2>Create</h2>


    @using (Html.BeginForm("Create", "Tbl_Employee", null, FormMethod.Post, new { enctype = " 
    multipart/form-data" }))
   {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <h4>Tbl_Employee</h4>
            <hr /> 
    <div class="form-group">
                @Html.LabelFor(model => model.SignaturePath, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
               <input id="signaturePath " title ="upload images " type="file" name="file" />
               @Html.ValidationMessageFor(model => model.SignaturePath, "", new { @class = "text-danger" }) 
            </div>
        </div> 

Here is the "Index view" code:

<td>
        @if (!string.IsNullOrWhiteSpace(item.SignaturePath))
        {
            <img width = "70" height = "50"
                 src="~/Content/Images/Employees/@Url.Content(item.SignaturePath)"
                 alt= "Alternate Text"/> }
</td>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城缀染半城烟沙 2025-01-29 09:07:23

上传的图像将转换为二进制格式,然后将其保存到SQL Server数据库表。

检查下面给定的链接以获取更多信息,如何从ASP.NET MVC中的SQL Server上载和检索映像,

The uploaded Images will be converted to Binary format then saved to SQL Server Database table.

Check the below given links for more information, how to upload and retrieve Images from Sql Server in ASP.NET MVC,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文