使用MVC 4.0上传照片

时间:2014-04-28 04:31:35

标签: asp.net-mvc-4 file-upload

我想上传照片并使用 Asp.Net MVC 4 保存在数据库中(框架必须是4.5)?我看到几个网络教程和视频,但无法获得实际答案。在这个问题中,用户也可以从设备中选择多张照片。 我的模型类是

 public class Picture
 {
    [Key]
    public int PictureId { set; get; }
    public IEnumerable<HttpPostedFileBase> Files { set; get; }
 }

  **My Controller is**


  namespace Picture_Rpload.Controllers
  {
  public class PictureController : Controller
  {
    private PictureDbContext db = new PictureDbContext();

    //
    // GET: /Picture/

    public ActionResult Index()
    {
        return View(db.Pictures.ToList());
    }

    //
    // GET: /Picture/Details/5

    public ActionResult Details(int id = 0)
    {
        Picture picture = db.Pictures.Find(id);
        if (picture == null)
        {
            return HttpNotFound();
        }
        return View(picture);
    }

    //
    // GET: /Picture/Create

    public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /Picture/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    //public ActionResult Create(Picture picture)
    //{
    //    if (ModelState.IsValid)
    //    {
    //        db.Pictures.Add(picture);
    //        db.SaveChanges();
    //        return RedirectToAction("Index");
    //    }

    //    return View(picture);
    // }

    public ActionResult Index(Picture picture)
    {
        foreach (var file in picture.Files)
        {
            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/images"), fileName);
                file.SaveAs(path);
            }
        }

        return RedirectToAction("Index");
    }

    //
    // GET: /Picture/Edit/5

    public ActionResult Edit(int id = 0)
    {
        Picture picture = db.Pictures.Find(id);
        if (picture == null)
        {
            return HttpNotFound();
        }
        return View(picture);
    }

    //
    // POST: /Picture/Edit/5

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Picture picture)
    {
        if (ModelState.IsValid)
        {
            db.Entry(picture).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(picture);
    }

    //
    // GET: /Picture/Delete/5

    public ActionResult Delete(int id = 0)
    {
        Picture picture = db.Pictures.Find(id);
        if (picture == null)
        {
            return HttpNotFound();
        }
        return View(picture);
    }

    //
    // POST: /Picture/Delete/5

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Picture picture = db.Pictures.Find(id);
        db.Pictures.Remove(picture);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}

}

2 个答案:

答案 0 :(得分:0)

一个简单的输入类型=&#34;文件&#34;在客户端将做到这一点。在您的控制器上,您将收到HttpPostedFileBase类型的模型/变量中的文件,确保您的表单具有multiform / part-data的内容类型(忘记确切的名称)

答案 1 :(得分:0)