我希望在数据库中上传我的文档。(MVC5,Varbinary)
首先,我将这些文档存储在服务器上的文件夹中(" ServeurPathFolder / IdUser"), 并且完美地工作=>
现在我希望使用相同的控制器将这些文件保存在数据库中。
=>使用调试模式,我很顺利在上传控制器中,但我的所有参数都是NULL,最后有一个空参考EXCEPTION你知道为什么吗?
public JsonResult Upload(FilesIndexViewModel model, HttpPostedFileBase files)
我的ViewModel
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);
}
}
感谢您的帮助!
答案 0 :(得分:0)
您对浏览器文件的输入元素应具有与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
的集合