2016-08-01 36 views
0

使用FileUpload控件這是我的模型類:HttpPostedFileBase獲得空當我在MVC

public class FileUploadModel 
    { 
     public HttpPostedFileBase File { get; set; } 
    } 

這是我的看法:

@using VidesExample.Models 
@model FileUploadModel 
@{ 
    ViewBag.Title = "FileUpload"; 
} 

<h2>FileUpload</h2> 
@using (Html.BeginForm("FileUpload","Sample")) 
{ 
    @Html.TextBoxFor(model=>model.File,new { type="file"}) 
    <input type="submit" value="Upload" /> 
} 

這是我的控制器:

public ActionResult FileUpload() 
     { 
      return View(); 
     } 
     [HttpPost] 
     public ActionResult FileUpload(FileUploadModel obj) 
     { 
      var file=obj.File; 
      // if (video.File.ContentLength <= 0) return null; 
      // var fileName = Path.GetFileName(video.File.FileName); 
      // if (fileName == null) return null; 
      // var path = Path.Combine(Server.MapPath("~/Videos"), fileName); 
      // video.File.SaveAs(path); 

      //// return fileName; 
     return View(); 
     } 

當我嘗試獲取文件時,它顯示空值。以及如何顯示以及如何使用Mvc存儲視頻。

回答

1

添加下面性能在表單標籤

method="post" enctype="multipart/form-data" 
@using (Html.BeginForm("ActioName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" })) 
+0

其working.Thank你 – sadasiva

相關問題