保存文件路径到数据库,只保存文件名

时间:2016-10-24 14:08:14

标签: c# html asp.net-mvc-4

好的所以我一直在搜索这些代码并且几乎所有东西都可以工作。我可以上传图像并将其保存到创建的文件夹中。我也可以保存其他值传递给数据库。我现在的问题是我想将文件路径保存到数据库,所以我可以调用它来在另一个页面上显示图像。出于某种原因,它只会保存正在上传的图像名称而不保存路径。

当我调试它并看到所有传递的内容时,我添加了一个手表  file.SaveAs(pathLoc);

  

表达式已经过评估,没有值无效

以下是我使用

编写的代码

控制器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(ImageInfo info, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
            if (file != null)
            {
                var pic = Path.GetFileName(file.FileName); 
                var pathLoc = Path.Combine(Server.MapPath("~/Uploads/") + pic);
                file.SaveAs(pathLoc);


                info.ImagePath = file.FileName;
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        return View(info);
    }

查看

 <h2>Create</h2>


@using (Html.BeginForm("Create", "ImageInfoes", null, FormMethod.Post,     new { enctype = "multipart/form-data" }))
 {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

<div class="form-horizontal">
    <h4>ImageInfo</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.LabelFor(model => model.ImageName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ImageName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ImageName, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.ImageSize, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ImageSize, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ImageSize, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.ImagePath, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input id="ImagePath" title="Upload a product image" type="file" name="file" />
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
 }

    <div>
       @Html.ActionLink("Back to List", "Index")
   </div>

模型

    public partial class ImageInfo
   {
      public byte id { get; set; }
      public string ImageName { get; set; }
      public Nullable<long> ImageSize { get; set; }
      public string ImagePath { get; set; }
   }

任何事情都会有所帮助。

1 个答案:

答案 0 :(得分:1)

如果需要,您需要将完整路径组合文件路径和文件名。

Console.WriteLine(Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, System.IO.Path.GetRandomFileName()));
  

C:\ Users \ 02483695 \ Documents \ Visual Studio   2015年\项目\ ConsoleApplication5 \缺点   oleApplication5 \ BIN \调试\ ConsoleApplication5.exe \ tv1k1uev.jsq

下面您可以找到有关文件,文件目录,文件路径和文件扩展名的示例。

System.IO.FileInfo fileInfo = new FileInfo("filename.file");
var fileDirectory = fileInfo.DirectoryName;
var fileName = fileInfo.Name;
var fileExtension = fileInfo.Extension;
var filePathandFileNameBoth = fileInfo.FullName;

Console.WriteLine("filePathandFileNameBoth: ");
Console.WriteLine(filePathandFileNameBoth);
Console.WriteLine("-");
Console.WriteLine("fileDirectory:");
Console.WriteLine(fileDirectory);
Console.WriteLine("-");
Console.WriteLine("fileName:");
Console.WriteLine(fileName);
Console.WriteLine("-");
Console.WriteLine(filePathandFileNameBoth == fileDirectory + "\\" + fileName ? fileExtension : "I'm totally wrong");
Console.ReadLine();

结果: Console Output