Update according to OP comment
set the Max file length in the web.config file
Change the "?" to a file size that you want to be your max, for example 65536 is 64MB
<configuration>
<system.web>
<httpRuntime maxRequestLength="?" />
</system.web>
</configuration>
You can't add the file to the model, it will be in it's own field not part of the model
<input name="videoUpload" type="file" />
Your action is incorrect. It needs to accept the file as it's own parameter (or if multiple use IEnumerable<HttpPostedFileBase>
as the parameter type)
[HttpPost]
public ActionResult Create(VideoUploadModel VM, HttpPostedFileBase videoUpload)
{
if (ModelState.IsValid)
{
if(videoUpload != null) { // save the file
var serverPath = server.MapPath("~/files/" + newName);
videoUpload.SaveAs(serverPath);
}
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
return View(VM);
}
If you were allowing multiple files to be selected you have to allow for that
[HttpPost]
public ActionResult Create(VideoUploadModel VM, IEnumerable<HttpPostedFileBase> videoUpload)
{
if (ModelState.IsValid)
{
if(videoUpload != null) { // save the file
foreach(var file in videoUpload) {
var serverPath = server.MapPath("~/files/" + file.Name);
file.SaveAs(serverPath);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
return View(VM);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…