【问题标题】:ASPNET MVC5, Files Upload Database, VarbinaryASPNET MVC5,文件上传数据库,Varbinary
【发布时间】:2016-08-17 14:39:12
【问题描述】:

我希望将我的文档上传到数据库中。(MVC5,Varbinary)

首先,我将这些文档存储在服务器上的一个文件夹中(“ServeurPathFolder/IdUser”), 而且效果很好 =>

现在我希望用同一个控制器将这些文件保存在数据库中。

=> 使用 Debug 模式,我在 UploadController 中恢复得很好,但我的所有参数都是 NULL,最后出现 NULL REFERENCE EXCEPTION 你知道为什么吗?

  public JsonResult Upload(FilesIndexViewModel model, HttpPostedFileBase files)

我的视图模型

public class FilesIndexViewModel
    {
        [Required]
        public HttpPostedFileBase File { get; set; }

        public byte ID_TYPE { get; set; }

        public byte ID_CR{ get; set; }

        public string ID_CREATEUR { get; set; }

        public DateTime DT_CREATION { get; set; }

        public string LIB { get; set; }

        public string TEXT { get; set; }

        public string CD_CONTENT_TYPE { get; set; }
    }

我的观点(_Files)

@using (Html.BeginForm("Upload", "FileUpload", FormMethod.Post, new { encType = "multipart/form-data" }))
{
    <form id="fileupload">
        @*<form id="fileupload" method="POST" enctype="multipart/form-data" data-url="@Url.Action("Upload", "FileUpload")">*@
            <div class="row fileupload-buttonbar">
                <div class="col-lg-7">
                    <!-- The fileinput-button span is used to style the file input field as button -->
                    <span class="btn btn-success fileinput-button">
                        <i class="glyphicon glyphicon-plus"></i>
                        <span>Add files...</span>
                        <input type="file" name="files[]" multiple>
                    </span>
                    <button type="submit" class="btn btn-primary start">
                        <i class="glyphicon glyphicon-upload"></i>
                        <span>Start upload</span>
                    </button>
                    <button type="reset" class="btn btn-warning cancel">
                        <i class="glyphicon glyphicon-ban-circle"></i>
                        <span>Cancel upload</span>
                    </button>
                    <button type="button" class="btn btn-danger delete">
                        <i class="glyphicon glyphicon-trash"></i>
                        <span>Delete</span>
                    </button>
                    <input type="checkbox" class="toggle">
                    <!-- The global file processing state -->
                    <span class="fileupload-process"></span>
                </div>
                <!-- The global progress state -->
                <div class="col-lg-5 fileupload-progress fade">
                    <!-- The global progress bar -->
                    <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                        <div class="progress-bar progress-bar-success" style="width:0%;"></div>
                    </div>
                    <!-- The extended global progress state -->
                    <div class="progress-extended">&nbsp;</div>
                </div>
            </div>
            <!-- The global progress state -->
            <div class="col-lg-5 fileupload-progress fade">
                <!-- The global progress bar -->
                <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                    <div class="progress-bar progress-bar-success" style="width:0%;"></div>
                </div>
                <!-- The extended global progress state -->
                <div class="progress-extended">&nbsp;</div>
            </div>

            <!-- The table listing the files available for upload/download -->
            <table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>
        </form>

还有我的控制器

[AcceptVerbs(HttpVerbs.Post)]
[HttpPost]
public JsonResult Upload(FilesIndexViewModel model, HttpPostedFileBase files)
{
    var uploadedFile = (model.File != null && model.File.ContentLength > 0) ? new byte[model.File.InputStream.Length] : null;
    if (uploadedFile != null)
    {
        model.File.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
    }
    CTG_DOCUMENT image = new CTG_DOCUMENT
    {
        ID_TYPE = model.ID_TYPE,
        ID_CR = model.ID_CR,
        ID_CREATEUR = model.ID_CREATEUR,
        DT_CREATION = DateTime.Now,
        LIB = model.LIB,
        DOCUMENT = uploadedFile,
        CD_CONTENT_TYPE = model.CD_CONTENT_TYPE
    };
    db.CTG_DOCUMENT.Add(image);
    db.SaveChanges();

    var resultList = new List<ViewDataUploadFilesResult>();

    var CurrentContext = HttpContext;

    filesHelper.UploadAndShowResults(CurrentContext, resultList);
    JsonFiles Jfiles = new JsonFiles(resultList);

    bool isEmpty = !resultList.Any();


    if (isEmpty)
    {
        return Json("Error ");
    }
    else
    {              
        return Json(Jfiles);
    }
}

感谢您的帮助!

【问题讨论】:

    标签: database file asp.net-mvc-5 varbinary


    【解决方案1】:

    浏览器文件的输入元素的名称属性值应与HttpPostedFileBase 类型的操作方法参数相同。

    所以更新你的剃刀视图代码,输入元素名称为"files"

    <input type="file" name="files" multiple>
    

    另外,我看到您在文件输入元素上有多个属性。如果您喜欢发送多个文件,您应该将您的操作方法参数更新为 HttpPostedFileBase 的集合。

    [HttpPost]
    public JsonResult Upload(FilesIndexViewModel model,IEnumerable<HttpPostedFileBase> files)
    {
      // loop through files after making sure it is not null
    }
    

    我还注意到,您的视图模型中已经有一个 HttoPostedFileBase 类型的属性。在这种情况下,您不需要在 HttpPost 操作方法中包含第二个参数。

    你的动作方法可以是

    [HttpPost]
    public JsonResult Upload(FilesIndexViewModel model)
    {
      //check model.File property value
    }
    

    另外,正如我之前提到的,如果你想支持多个文件上传,只需将视图模型的这个属性更改为 HttpPostedFile 的集合

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-15
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 2017-03-09
      • 2015-05-29
      • 1970-01-01
      相关资源
      最近更新 更多