2016-08-17 14 views
0

。(MVC5、VARBINARY)ASPNET MVC5、ファイルのアップロードデータベース、VARBINARY

が最初に私は、サーバー上のフォルダにこれらの文書を保存する(「ServeurPathFolder/IdUser」 )、 それは完璧に動作します=>Screenshot

私はこれらのファイルを同じコントローラでデータベースに保存します。

=>デバッグモードでは、私はうまくいく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">&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); 
    } 
} 
public JsonResult Upload(FilesIndexViewModel model, HttpPostedFileBase files) 

MyDatabase

私の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; } 
    } 

ご協力ありがとうございます!

答えて

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アクションメソッドに2番目のパラメータを持つ必要はありません。

とあなたのアクションメソッドは

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

私は先に述べたように、あなたがmulipleファイルのアップロードをサポートしたい場合も、単純HttpPostedFileのコレクションにあなたのビューモデルのこのプロパティを変更することができ