2016-08-09 3 views
0

私はEntityフレームワークを使用しているMVCアプリケーションを作成しています。最終的にファイルのアップロードと表示を作成したい。私は実際に私は、エンティティフレームワークモデルとAMを使用していますエラーにMVCでファイルのアップロードと表示ができない

{"Object reference not set to an instance of an object."} 
Inner Exception null 

を取得私はアップロードfile.clickビューボタンを開くには、[表示]ボタンを持ってプロジェクトを作成した後、コントローラプロジェクトコントローラーは、アップロード

public async Task<ActionResult> FileManager(int ProjectId = 0) 
    { 
     ReadCookieValue cookie = new ReadCookieValue(); 

     clientId = cookie.readuserinfo().ClientId; 
     userId = cookie.readuserinfo().Id; 
     roleId = cookie.readuserinfo().RoleId; 
     var model = await _project.FilesList(ProjectId, clientId); 
     return View(model); 
    } 
    public async Task<ActionResult> FileUpload(int ProjectId = 0) 
    { 
     return View(ProjectId); 
    } 
    [HttpPost] 
    public async Task<ActionResult> FileUploadHandler(int ProjectId = 0) 
    { 
     ReadCookieValue cookie = new ReadCookieValue(); 

     clientId = cookie.readuserinfo().ClientId; 
     userId = cookie.readuserinfo().Id; 
     roleId = cookie.readuserinfo().RoleId; 
     if (ProjectId != 0) 
     { 
      foreach (var fileKey in Request.Files.AllKeys) 
      { 
       ProjectFileModel model = null; 
       var file = Request.Files[fileKey]; 
       try 
       { 
        if (file != null) 
        { 
         model = new ProjectFileModel(); 
         var fileName = Path.GetFileName(file.FileName); 
         var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), clientId.ToString(), ProjectId.ToString()); 
         if (!Directory.Exists(path)) 
         { 
          Directory.CreateDirectory(path); 
         } 
         var path1 = Path.Combine(path, fileName); 
         model.ProjectId = ProjectId; 
         model.FileName = fileName; 
         model.FilePath = path; 
         model.UploadedBy = User.Identity.Name; 
         model.UploadedDate = DateTime.UtcNow; 
         model.ClientId = clientId; 
         var result = await _project.UploadFiles(model); 
         file.SaveAs(path1); 

        } 
       } 
       catch (Exception ex) 
       { 
        return Json(new { Message = "Error in saving file" }); 
       } 
      } 
     } 
     else 
     { 
      return Json(new { Message = "Please Select Project" }); 
     } 
     return Json(new { Message = "File saved" }); 
    } 

を提出しなければなりません自分のモデルを使ってFileUploadHandlerコントローラAction.Here FileuploadHandlerコードに

public async Task<ActionResult> FileUploadHandler(int ProjectId = 0) 
     { 
      ReadCookieValue cookie = new ReadCookieValue(); 

      clientId = cookie.readuserinfo().ClientId; 
      userId = cookie.readuserinfo().Id; 
      roleId = cookie.readuserinfo().RoleId; 
      if (ProjectId != 0) 
      { 
       foreach (var fileKey in Request.Files.AllKeys) 
       { 
        ProjectFileModel model = null; 
        var file = Request.Files[fileKey]; 
        try 
        { 
         if (file != null) 
         { 
          model = new ProjectFileModel(); 
          var fileName = Path.GetFileName(file.FileName); 
          var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), clientId.ToString(), ProjectId.ToString()); 
          if (!Directory.Exists(path)) 
          { 
           Directory.CreateDirectory(path); 
          } 
          var path1 = Path.Combine(path, fileName); 
          model.ProjectId = ProjectId; 
          model.FileName = fileName; 
          model.FilePath = path; 
          model.UploadedBy = User.Identity.Name; 
          model.UploadedDate = DateTime.UtcNow; 
          model.ClientId = clientId; 
          var result = await _project.UploadFiles(model); 
          file.SaveAs(path1); 

         } 
        } 
        catch (Exception ex) 
        { 
         return Json(new { Message = "Error in saving file" }); 
        } 
       } 
      } 
      else 
      { 
       return Json(new { Message = "Please Select Project" }); 
      } 
      return Json(new { Message = "File saved" }); 
     } 

前のこのコードを使用して、ここで

<div class="ibox-content"> 
<ng-form id="my-awesome-dropzone" class="dropzone" action="@Url.Action("FileUploadHandler", "Projects")" method="post" enctype="multipart/form-data"> 
<div class="dropzone-previews"></div> 
    <button type="submit" class="btn btn-primary pull-right">Submit this form!</button> 
    </ng-form> 

    </div> 

ファイルアップロードコードは、エンティティフレームワークアウトして開発します。後、私はモデルフォルダと

namespace Inspinia_MVC5.Entityframework 
{ 
    using System; 

    public partial class ProjectsList_Result 
    { 
     public int ProjectId { get; set; } 
     public string ProjectName { get; set; } 
     public Nullable<System.DateTime> StartDate { get; set; } 
     public Nullable<System.DateTime> EndDate { get; set; } 
     public Nullable<bool> IsActive { get; set; } 
     public Nullable<System.DateTime> CreatedDate { get; set; } 
     public string CreatedBy { get; set; } 
     public Nullable<System.DateTime> UpdatedDate { get; set; } 
     public string UpdatedBy { get; set; } 
     public int ClientId { get; set; } 
     public string Employees { get; set; } 
     public bool IsAnalyticsStart { get; set; } 
     public bool IsAnalyticsEnd { get; set; } 
     public string ReportType { get; set; } 
    } 
} 

下EDMX見ることができますクラスの2つのモデルタイプを見てきました。この作成したエンティティフレームワークモデルを取る私は、エンティティフレームワークモデルタイプなし

namespace Inspinia_MVC5.Models 
{ 
    public class ProjectsModel 
    { 
     public int ProjectId { get; set; } 
     public string ProjectName { get; set; } 
     [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
     public DateTime StartDate { get; set; } = DateTime.Now; 
     [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
     public DateTime EndDate { get; set; } = DateTime.Now; 
     public bool IsActive { get; set; } 
     public DateTime CreatedDate { get; set; } 
     public string CreatedBy { get; set; } 
     public DateTime UpdatedDate { get; set; } 
     public string UpdatedBy { get; set; } 
     public int ClientId { get; set; } 
     public List<LoginModelViewModel> AllEmployees { get; set; } 
     public string Employees { get; set; } 
     public bool IsAnalyticsStart { get; set; } 
     public bool IsAnalyticsEnd { get; set; } 
     public List<ReportType> reportTypes { get; set; } 
     public string ReportType { get; set; } 
     public string Designation { get; set; } 
    } 
} 
をEntity Frameworkの内部の従来の方法に従ってきました私の解決のフォルダの

私はApp_start/Uploads/1/1を見た。コードを実行するとエラーが発生する

{"Object reference not set to an instance of an object."} 
    Inner Exception null 

私は何をしたのですか?誰もこの問題を助けてください?

+0

を使用していますと仮定は、あなたが上でデバッガを使用していますサーバーはnullが何かを確認するには?それはクッキーかもしれない?まあそれは何でもできる! – Heberda

+0

私はエンティティモデルと自分のモデルを同時に使用できますか? –

+0

はい、どのような意味ですか? Visual Studioを使用していますか?例外がスローされると、ローカルホストを使用したデバッグが停止し、ヌル要素を検査できます。それは私達が必要とする情報です:) – Heberda

答えて

-1

ファイルのアップロード: - 私はあなたがアップロードファイルのためのコードの記述方法の例を取る示しています

私はビデオ表内のファイルをアップロードし、PostVideoモデル

[HttpPost] 
public ActionResult PostAVideo(PostVideo model, HttpPostedFileBase file) 
    { 
     if (ModelState.IsValid && file.ContentLength > 0) 
     { 
      string filePath = "/Uploads/" + WebSecurity.CurrentUserName + "/" + model.Category; 
      if (!Directory.Exists(Server.MapPath(filePath))) 
      { 
       Directory.CreateDirectory(Server.MapPath(filePath)); 
      } 

      var fileName = Path.GetFileName(file.FileName); 
      var path = Path.Combine(Server.MapPath(filePath), fileName); 
      file.SaveAs(path); 

      Video _model = new Video(); 
      _model.Title = model.Title; 
      _model.Path = filePath + "/" + fileName; 
      _model.Keyword = model.Keyword; 
      _model.Description = model.Description; 
      _model.Categories = model.Category; 
      _model.CreatedDate = model.CreatedDate; 
      _model.UserName = User.Identity.Name; 
      _model.UserId = WebSecurity.CurrentUserId; 
      _model.Name = fileName; 
      _model.IsDownload = model.IsDownload; 
      _model.IsCommented = model.IsComment; 
      _model.Poster = WebSecurity.CurrentUserName; 
      db.Video.Add(_model); 
      db.SaveChanges(); 
      ModelState.Clear(); 


     } 

     return View(); 
    } 
+0

あなたの答えは、あなたがこのようにしなければならない理由と、それが質問の中のコードとどのように違うのかを説明することによって大きく利益を得るでしょう。 –

関連する問題