这是我的模特课:
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显示和如何存储视频。
答案 0 :(得分:1)
在表单标记
中的属性下添加method="post" enctype="multipart/form-data"
@using (Html.BeginForm("ActioName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))