在ASP.NET MVC 5 EF6中上载图像

时间:2022-09-18 08:28:41

I've had a look at some other questions and tutorials, but I'm struggling to incorporate the suggestions into my existing work.

我已经看了一些其他问题和教程,但我很难将这些建议纳入我现有的工作中。

I want the user to be able to upload an image in a submission form for a new listing, my listing model looks like this;

我希望用户能够在提交表单中上传新图片的图片,我的列表模型如下所示;

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; }

}

I have a separate controller, the create method is as follows;

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

// 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);
    }

Any direction on how I can implement this seemingly fairly simple task into the my existing code would be greatly appreciated!

关于如何在现有代码中实现这个看似相当简单的任务的任何方向都将不胜感激!

1 个解决方案

#1


0  

I found a solution that works that allows me to upload a single file to an Uploads directory.

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

My model has remained unchanged.

我的模型保持不变。

The controller was updated to include;

控制器已更新为包括;

HttpPostedFileBase file

And;

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);

        }

Which in context of my original code forms this;

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

// 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);
    }

In my create view, I added;

在我的创建视图中,我补充说;

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

And;

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

This works for me, however, I've not tested to see how it performs when attempting to view each individual listing ID, so this might not be the correct solution for me.

这对我有用,但是,我没有测试过它在尝试查看每个列表ID时是如何执行的,所以这对我来说可能不是正确的解决方案。

#1


0  

I found a solution that works that allows me to upload a single file to an Uploads directory.

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

My model has remained unchanged.

我的模型保持不变。

The controller was updated to include;

控制器已更新为包括;

HttpPostedFileBase file

And;

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);

        }

Which in context of my original code forms this;

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

// 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);
    }

In my create view, I added;

在我的创建视图中,我补充说;

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

And;

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

This works for me, however, I've not tested to see how it performs when attempting to view each individual listing ID, so this might not be the correct solution for me.

这对我有用,但是,我没有测试过它在尝试查看每个列表ID时是如何执行的,所以这对我来说可能不是正确的解决方案。