【发布时间】: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"> </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"> </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