【问题标题】:Storing Physical Path on database instead of virtual one将物理路径存储在数据库而不是虚拟路径上
【发布时间】:2016-04-25 19:14:42
【问题描述】:

我认为我的问题很简单,但我正在努力解决它。 当我上传图像文件时,它工作正常,但数据库上的路径是物理路径,如 C://user/pictures/blabla.... 。 我想把它改成虚拟路径(~/...)。 当我手动将数据库路径更改为虚拟路径时,将显示图像。 提前感谢您的帮助。 这是我的代码:

控制器

public ActionResult Create(DriverReg model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var phisicalPath = Path.Combine(Server.MapPath("~/Content/uploads/"), fileName);
                    file.SaveAs(phisicalPath);
                    DriverReg newRecord = new DriverReg();
                    newRecord.FullName = model.FullName;
                    newRecord.Address = model.Address;
                    newRecord.Postcode = model.Postcode;
                    newRecord.Contact = model.Contact;
                    newRecord.Email = model.Email;
                    newRecord.County = model.County;
                    newRecord.File = phisicalPath;
                    newRecord.Date = DateTime.Now;

                    db.DriverRegs.Add(newRecord);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }

            return View(model);
        }

创建视图

<div class="form-group">
                @Html.LabelFor(model => model.File, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <input type="file" class="file-input" name="file" />
                    @Html.ValidationMessageFor(model => model.File, "", new { @class = "text-danger" })
                </div>
            </div>

详情查看

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

            <dd>
                @Html.Image(@Model.File, "image")
            </dd>

【问题讨论】:

    标签: c# .net asp.net-mvc entity-framework razor


    【解决方案1】:

    在调用 Server.MapPath 之前存储虚拟路径,然后在数据库记录中使用它。

    var fileName = Path.GetFileName(file.FileName);
    var combinedVirtualPath = Path.Combine("~/Content/uploads", fileName);
    var phisicalPath = Server.MapPath(combinedVirtualPath);
    file.SaveAs(phisicalPath);
    DriverReg newRecord = new DriverReg();
    newRecord.FullName = model.FullName;
    newRecord.Address = model.Address;
    newRecord.Postcode = model.Postcode;
    newRecord.Contact = model.Contact;
    newRecord.Email = model.Email;
    newRecord.County = model.County;
    newRecord.File = combinedVirtualPath;
    newRecord.Date = DateTime.Now;
    

    【讨论】:

    • 谢谢斯蒂芬,我试过你的代码,但没有用,它会导致错误文件是物理路径,但应该是虚拟路径
    • 谢谢大家,Ashley 的更新非常完美。你拯救了我的一天
    【解决方案2】:

    将虚拟路径分配给变量怎么样?

    var fileName = Path.GetFileName(file.FileName);
    var destination = $"~/Content/uploads/{fileName}";
    var physicalPath = Server.MapPath(destination);
    file.SaveAs(physicalPath);
    

    然后只存储destination变量的内容

    【讨论】:

    • 谢谢 DVK,我已经尝试过你的答案,但仍然是 db 上的物理路径,还有其他建议吗?
    【解决方案3】:
    var phisicalPath = Path.Combine(("~/Content/uploads/"), fileName);
    

    或

    string filePath = @"~/Content/uploads/"
    var phisicalPath = Path.Combine(filePath, fileName);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多