2015-11-06 265 views
0

我可以上传图片。但是我没有保存数据库的路径。我怎样才能做到这一点?如何将图像路径保存到数据库? MVC。

我删除了一些视图中代码较短的函数。我希望一切都被理解。

这里我得到了什么:

MODEL:

 public class Ogloszenie 
    { 
     [Key, ForeignKey("Pojazd")] 
     public int PojazdOgloszenieId { get; set; } 
     public RodzajPaliwa RodzajPaliwa { get; set; } 
     public int RokProdukcji { get; set; } 
     public int MocSilnika { get; set; } 
     public int Przebieg { get; set; } 
     public DateTime DataPrzegladu { get; set; } 
     public DateTime DataUbezpieczenia { get; set; } 
     public string OpisPojazdu { get; set; } 

//path 
     public string Zdjecie { get; set; } 
//path 

     public virtual Pojazd Pojazd { get; set; } 
    } 

控制器:

 [HttpPost] 
      [ValidateAntiForgeryToken] 
      public ActionResult Create([Bind(Include = "PojazdOgloszenieId,RodzajPaliwa,RokProdukcji,MocSilnika,Przebieg,DataPrzegladu,DataUbezpieczenia,OpisPojazdu,Zdjecie")] Ogloszenie ogloszenie, HttpPostedFileBase file) 
      { 
       if (ModelState.IsValid) 
       { 
        if (file != null) 
        { 
         var fileName = Path.GetFileName(file.FileName); 
         var path = Path.Combine(Server.MapPath("~/Zdjecia/"), fileName); 
         file.SaveAs(path); 

//*********************?????????? Something like this?  
Zdjecie = Url.Content("~/Zdjecia/" + file); 
        } 

        db.Ogloszenia.Add(ogloszenie); 
        db.SaveChanges(); 
        return RedirectToAction("Index"); 
       } 

       ViewBag.PojazdOgloszenieId = new SelectList(db.Pojazdy, "ID", "Marka", ogloszenie.PojazdOgloszenieId); 
       return View(ogloszenie); 
      } 

VIEW:

@model AutoMonit.Models.Ogloszenie 

    <h2>Utwórz ogłoszenie</h2> 

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

     <div class="form-horizontal"> 
      <hr /> 
      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
      <div class="form-group"> 
       @Html.LabelFor(model => model.PojazdOgloszenieId, "PojazdOgloszenieId", htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.DropDownListFor(model => model.PojazdOgloszenieId, null, htmlAttributes: new { @class = "form-control" }) 
        @Html.ValidationMessageFor(model => model.PojazdOgloszenieId, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

*************** 
. 
. 
. 
*************** 

    //FILE UPLOADING 
       <label for="file">Filename:</label> 
       <input type="file" name="file" id="file" /> 



      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type="submit" value="Utwórz" class="btn btn-default" /> 
       </div> 
      </div> 
     </div> 
    } 

    <div> 
     @Html.ActionLink("Wróć", "Index") 
    </div> 
+0

Cześć,Piter。你可以写Zdjecie =“〜/ Zdjecia /”+ fileName。虽然这将使您的数据库信息绑定到您的服务器配置上,以图像根文件夹的相对路径。我将只保存文件名并稍后与C#中的相对路径(动态检索)结合起来。 – codeRecap

+0

你能说更多吗?我是初学者:( – DiPix

+0

好的,只保存文件的名称,不是完整的路径,后来当你需要检索它时,可以将它与“〜/ Zdjecia /”结合起来,结果你会得到“ 〜/ Zdjecia/myFileName.txt“,您可以在客户端将其用作图像URL。 – codeRecap

回答

0

好吧,我做它(感谢codeRecap灵感; ))

[HttpPost] 
      [ValidateAntiForgeryToken] 
      public ActionResult Create([Bind(Include = "PojazdOgloszenieId,RodzajPaliwa,RokProdukcji,MocSilnika,Przebieg,DataPrzegladu,DataUbezpieczenia,OpisPojazdu")] Ogloszenie ogloszenie, HttpPostedFileBase file) 
      { 
       if (ModelState.IsValid) 
       { 
        if (file != null) 
        { 
         var fileName = Path.GetFileName(file.FileName); 
         var path = Path.Combine(Server.MapPath("~/Zdjecia/"), fileName); 
         file.SaveAs(path); 

ogloszenie.Zdjecie = Url.Content("~/Zdjecia/" + fileName); 

        } 

        db.Ogloszenia.Add(ogloszenie); 
        db.SaveChanges(); 
        return RedirectToAction("Index"); 
       } 

       ViewBag.PojazdOgloszenieId = new SelectList(db.Pojazdy, "ID", "Marka", ogloszenie.PojazdOgloszenieId); 
       return View(ogloszenie); 
      }