简体   繁体   English

ASP.Net MVC将.zip文件存储到数据库中作为模型的一部分?

[英]ASP.Net MVC store .zip files to database as part of model?

I'm trying to create an application that can store student assignments and allow me to view them and download them from the browser. 我正在尝试创建一个可以存储学生作业的应用程序,并允许我查看它们并从浏览器下载它们。 I would like to be able to upload a .zip file to the model, and have that be stored in the database with the rest of the items in the model. 我希望能够将.zip文件上传到模型,并将其与模型中的其余项一起存储在数据库中。 I don't know how to add file uploads or how to to store files to the database. 我不知道如何添加文件上传或如何将文件存储到数据库。 I managed to make my application to only store the id and student name to the database. 我设法使我的应用程序仅将ID和学生姓名存储到数据库中。

Here is how my model looks. 这是我的模型的外观。 I'm not sure what parameter to add here to that it allows my model to hold a .zip file which will be stored in the database, so right now I am storing everything else. 我不确定要在此添加什么参数,它是否允许我的模型保存将存储在数据库中的.zip文件,所以现在我将存储其他所有内容。

Assignment: 分配:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace ProjectVoting.Models
{
    public class Assignment
    {
        [Key]
        public int Id { get; set; }
        public string StudentName { get; set; }
        public int Votes { get; set; }

    }
}

And here is how my controller looks. 这是我的控制器的外观。 It allows all CRUD operations for what I have in the model as of right now. 到目前为止,它允许我对模型中的内容进行所有CRUD操作。

AssignmentController: AssignmentController:

using ProjectVoting.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace ProjectVoting.Controllers
{
    public class AssignmentController : Controller
    {

        ApplicationDbContext context;

        public AssignmentController()
        {
            context = new ApplicationDbContext();
        }

        // GET: Assignment
        public ActionResult Index()
        {
            return View(context.Assignments.ToList());
        }

        // GET: Assignment/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Assignment/Create
        [HttpGet]
        public ActionResult Create()
        {

            return View();
        }

        // POST: Assignment/Create
        [HttpPost]
        public ActionResult Create(Assignment assignment)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    context.Assignments.Add(assignment);
                    context.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(assignment);
            }
            catch (Exception e)
            {
                return View();
            }

        }

        // GET: Assignment/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            Assignment assignment = context.Assignments.Find(id);
            if (assignment == null)
                return HttpNotFound();
            return View(assignment);
        }

        // POST: Assignment/Edit/5
        [HttpPost]
        public ActionResult Edit(Assignment assignment)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    context.Entry(assignment).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(assignment);
            }
            catch
            {
                return View();
            }
        }

        // GET: Assignment/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            Assignment assignment = context.Assignments.Find(id);
            if (assignment == null)
                return HttpNotFound();
            return View(assignment);
        }

        // POST: Assignment/Delete/5
        [HttpPost]
        public ActionResult Delete(int? id, Assignment ass)
        {
            try
            {
                Assignment assignment = new Assignment();
                if (ModelState.IsValid)
                {
                    if (id == null)
                        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                    assignment = context.Assignments.Find(id);
                    if (assignment == null)
                        return HttpNotFound();
                    context.Assignments.Remove(assignment);
                    context.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(assignment);
            }
            catch
            {
                return View();
            }
        }
    }
}

How can I go changing the code in my model and controller, so that in when I get to my 'Create' view, there is a file upload option with a 'Browse' button form where I can select a .zip file. 如何更改模型和控制器中的代码,以便在进入“创建”视图时出现一个带有“浏览”按钮形式的文件上传选项,可以在其中选择一个.zip文件。 Then in that page, when I hit submit, it stores the id, name, votes as well as the .zip file into the database. 然后在该页面中,当我点击“提交”时,它将ID,名称,投票以及.zip文件存储到数据库中。 When I view the files in the index, I would like to see the .zip file and have the option to download it. 当我查看索引中的文件时,我想查看.zip文件并可以选择下载它。

How can I go about adding this functionality? 如何添加此功能?

If you want to store binary data in DB, you need to add in your model new property 如果要将二进制数据存储在数据库中,则需要在模型中添加新属性

    public byte[] File { get; set; }

You need to modify Create action: 您需要修改创建动作:

    [HttpPost]
    public ActionResult Create(Assignment assignment,HttpPostedFileBase file)
    {

        try
        {
            if (ModelState.IsValid)
            {
                if (file.ContentLength > 0) {
                     MemoryStream target = new MemoryStream();
                     file.InputStream.CopyTo(target);
                     byte[] data = target.ToArray();
                     assignment.File = data
                }
                context.Assignments.Add(assignment);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(assignment);
        }
        catch (Exception e)
        {
            return View();
        }

    }

In your "Create" view you need to add enctype="multipart/form-data" to your form and add inside your form 在“创建”视图中,您需要向表单添加enctype =“ multipart / form-data”并在表单内部添加

<input type="file" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM