使用Entities Asp Net MVC C#从数据库显示图像文件

时间:2016-04-19 13:06:02

标签: c# asp.net asp.net-mvc razor asp.net-mvc-5

我知道这不是一个新问题,但我已经查看了相关的所有主题,并且找不到我的答案。 我的问题是我已将图像数据存储到sql db中,但无法在编辑或索引(MVC 5)上显示它们。能提前帮助我吗? 这是我的代码:

CONTROLLER

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "DrvId,FullName,Address,Postcode,Contact,Email,County,File,Date")] DriverReg driverReg, HttpPostedFileBase[] files)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    /*Lopp for multiple files*/
                    foreach (HttpPostedFileBase file in files)
                    {
                        /*Geting the file name*/
                        string filename = System.IO.Path.GetFileName(file.FileName);
                        /*Saving the file in server folder*/
                        file.SaveAs(Server.MapPath("~/Content/UploadedFiles/" + filename));
                        string filepathtosave = "UploadedFiles/" + filename;
                        /*HERE WILL BE YOUR CODE TO SAVE THE FILE DETAIL IN DATA BASE*/
                    }

                    ViewBag.Message = "File Uploaded successfully.";
                }
                catch
                {
                    ViewBag.Message = "Error while uploading the files.";
                }

                db.DriversReg.Add(driverReg);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(driverReg);
        }

MODEL

namespace HopeRemovalsFinal.Models
{
    public class DriverReg
    {
        [Key]
        public int DrvId { get; set; }
        public string FullName { get; set; }
        public string Address { get; set; }
        public string Postcode { get; set; }
        public string Contact { get; set; }
        public string Email { get; set; }
        public string County { get; set; }
        [DataType(DataType.Upload)]
        [Display(Name ="Upload File")]
        [Required(ErrorMessage ="Please choose file to upload")]
        public string File { get; set; }
    //    public string FileName { get; set; }
        public DateTime Date { get; set; }

    }
    public class DriverDbContext : DbContext
    {
        public DriverDbContext()
            : base("VanRemovals")
        {
        }

        public static DriverDbContext Create()
        {
            return new DriverDbContext();
        }

          public DbSet<DriverReg> DriversReg { get; set; }

    }
}

DETAILS VIEW

            <dt>
                @Html.DisplayNameFor(model => model.File)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.File)
            </dd>

数据库结果

 Create New
FullName    Address     Postcode    Contact     Email   County  Upload File     Date    
John    manchester  M32 7DD     098764587   john@hotm.com   Lancashire  uk_map.png  19/04/2016 00:00:00     Edit | Details | Delete

UploadedFiles文件夹为空,但文件已保存在db上。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

我在您的代码中发现了几个错误。所以......这是我的解决方案:

型号:

[DataType(DataType.Upload)]
[Display(Name = "Upload File")]
[Required(ErrorMessage = "Please choose file to upload")]
[NotMapped]
public HttpPostedFileBase File { get; set; }

public string FileName { get; set; }

查看:

@Html.TextBoxFor(model => model.File, new { type = "file" })

控制器:

public ActionResult Create(DriverReg driverReg)
{
    ...
    var file = driverReg.File;
    var fileName = "~/Content/UploadedFiles/" + file.FileName;
    file.SaveAs(Server.MapPath(fileName));
    driverReg.FileName = fileName;
    ...
}

详情视图:

<dt>
    @Html.DisplayNameFor(model => model.File)
</dt>
<dd>
    <img src="@Model.FileName" />
</dd>

答案 1 :(得分:0)

Create允许多个文件似乎很奇怪,而class DriverReg只有string File(或FileName - 目前尚不清楚)不是一个集合。

我会说更改Create方法来接受单个文件对象 如有必要,还可以将表单调整为仅发布/上传一个文件。

然后,在Create内更改此内容:

string filepathtosave = "UploadedFiles/" + filename;

到此:

driverReg.File = "UploadedFiles/" + filename;

您可能需要更改我们在此处未看到的内容,但我认为完成上述所有操作应该会使其更好。