。(MVC5、VARBINARY)ASPNET MVC5、ファイルのアップロードデータベース、VARBINARY
が最初に私は、サーバー上のフォルダにこれらの文書を保存する(「ServeurPathFolder/IdUser」 )、 それは完璧に動作します=>
私はこれらのファイルを同じコントローラでデータベースに保存します。
=>デバッグモードでは、私はうまくいくUploadControllerでは、すべてのパラメータがNULLで、最後にNULL REFERENCE EXCEPTIONがありますか?マイビュー(_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);
}
}
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; }
}
ご協力ありがとうございます!