无法弄清楚如何将文件上传/下载到数据库MVC表单

时间:2018-08-23 20:11:11

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

我在MVC中有一个项目,我希望用户能够将文件(图像文件和仅一个文件)上载到表单上的数据库。按照相同的约定,他们应该能够下载他们上传的文件。我还希望他们能够编辑表单,其中包括上载其他文件(从而更改数据库中的文件。)

我已经看到了很多有关上传文件的主题,但是它们似乎处理将它们上传到文件服务器,或者上传到数据库,但是没有使用MVC方法以我需要的方式上传到数据库它。

据我了解,我首先要编辑模型以包含文件类型的功能,所以...

Report.cs

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

namespace BugTracker.Models
{
    public class Report
    {
        public int ID { get; set; }
        public String Name { get; set; }
        public SOMETHING File {get; set; }

    }
}

当然,在上面的代码中,“ SOMETHING”将替换为文件的数据类型。

我认为我不需要在控制器部分做任何事情。但是对于表单,我不确定要做什么,请查看Html.BeginForm部分,这是我正在使用的内容:

UserReportForm.cshtml

@using (Html.BeginForm("Save", "Report")
{
  \\...other fields of my form
  <div class="form-group">
    <label>Upload your file</label>
    @Html.SomeKindOfFileControl(m => m.Report.File, new {@class = "form-control"})
  </div>
}

}

基本上,我希望我的表单将此文件视为要输入到表中的另一段数据。在另一个View上,我希望用户能够检索它(我想我可以自己弄清楚这一点)。

我可以在模型和视图(用于表单)中放些什么来实现此功能?请谢谢!

0 个答案:

没有答案
相关问题