2017-04-14 81 views
2

我看了一些其他问题和教程,但我努力将这些建议融入到我现有的工作中。上传图像在ASP.NET MVC 5 EF6

我希望用户能够在提交表单中上传新图片,我的列表模型看起来像这样;

public class Listing 
{ 
    public int ListingID { get; set; } 
    [Display(Name = "Select Category")] 
    public int CategoryID { get; set; } 
    [Display(Name = "Select Vendor")] 
    public int VendorID { get; set; } 
    [Required] 
    [Display(Name = "Listing Name")] 
    public string ListingName { get; set; } 
    [Required] 
    [Display(Name = "Listing Description")] 
    public string ListingDesc { get; set; } 
    [Required] 
    [Display(Name = "Maximum Capacity")] 
    public int Capacity { get; set; } 
    [Required] 
    [DataType(DataType.Currency)] 
    [Display(Name = "Price")] 
    public decimal Price { get; set; } 
    [Required] 
    [Display(Name = "Fixed Price?")] 
    public bool IsFixedPrice { get; set; } 
    [Required] 
    public string Address { get; set; } 
    [Display(Name = "Address Line 2")] 
    public string Address2 { get; set; } 
    [Required] 
    public string City { get; set; } 
    [Required] 
    public string Postcode { get; set; } 

    public virtual Category Category { get; set; } 
    public virtual Vendor Vendor { get; set; } 

} 

我有一个单独的控制器,创建方法如下;

// GET: Listing/Create 
    public ActionResult Create() 
    { 
     ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName"); 
     ViewBag.VendorID = new SelectList(db.Vendors, "ID", "CompanyName"); 
     return View(); 
    } 

    // POST: Listing/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "ListingID,CategoryID,VendorID,ListingName,ListingDesc,Capacity,Price,IsFixedPrice,Address,Address2,City,Postcode")] Listing listing) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Listings.Add(listing); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", listing.CategoryID); 
     ViewBag.VendorID = new SelectList(db.Vendors, "ID", "CompanyName", listing.VendorID); 
     return View(listing); 
    } 

我如何能实现这个看似很简单的任务到我的现有代码的任何方向将不胜感激!

+0

您是否尝试过使用输入文件类型标记并将其绑定到您的模型的新属性?你应该能够做到这一点,并对待其他所有事情几乎一样。 – DevNoob

回答

0

我找到了一个解决方案,它可以让我上传单个文件到上传目录。

我的模型保持不变。

控制器已更新为包括;

HttpPostedFileBase file 

而且;

if (file.ContentLength > 0) 
     { 
      string fileName = Path.GetFileName(file.FileName); 
      string directory = Server.MapPath("~/Content/uploads/"); 
      if (!Directory.Exists(directory)) 
      { 
       Directory.CreateDirectory(directory); 
      } 
      string path = Path.Combine(directory, fileName); 
      file.SaveAs(path); 

     } 

在我的原始代码的上下文中形成这个;

// POST: Listing/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "ListingID,CategoryID,VendorID,ListingName,ListingDesc,Capacity,Price,IsFixedPrice,Address,Address2,City,Postcode")] Listing listing, HttpPostedFileBase file) 
    { 

     if (file.ContentLength > 0) 
     { 
      string fileName = Path.GetFileName(file.FileName); 
      string directory = Server.MapPath("~/Content/uploads/"); 
      if (!Directory.Exists(directory)) 
      { 
       Directory.CreateDirectory(directory); 
      } 
      string path = Path.Combine(directory, fileName); 
      file.SaveAs(path); 

      //var fileName = Path.GetFileName(file.FileName); 
      //var path = Path.Combine(Server.MapPath("~/Content/Uploads")); 
      //file.SaveAs(path); 
     } 

     if (ModelState.IsValid) 
     { 

      db.Listings.Add(listing); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", listing.CategoryID); 
     ViewBag.VendorID = new SelectList(db.Vendors, "ID", "CompanyName", listing.VendorID); 
     return View(listing); 
    } 

在我的创建视图中,我添加了;

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

而且;

<input type="file" name="file" id="file" /> 
<input type="submit" value="submit" /> 

这对我的作品,但是,我没有测试,看看试图查看每个单独上市ID时,它如何执行,所以这可能不是对我来说是正确的解决方案。