問題描述
在我的控制器中,因為我希望能夠填寫有關視頻的一些詳細信息并實際上傳它,所以 Video 類不需要實際的視頻,因為它將被傳遞給另一個 Web 服務.
In my controller I have, because I wanted to be able to fill out some details about the video and actually upload it, the Video class doesn't need the actual video because it's going to be passed to another web service.
public class VideoUploadModel
{
public HttpPostedFileBase vid { get; set; }
public Video videoModel { get; set; }
}
//
// POST: /Video/Create
[HttpPost]
public ActionResult Create(VideoUploadModel VM)
{
if (ModelState.IsValid)
{
db.Videos.AddObject(VM.videoModel);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
return View(VM);
}
在我看來我有
@model LifeHighlightsShoeLace.Controllers.VideoController.VideoUploadModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("Create", "Video", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Video</legend>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.KalturaID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.videoModel.KalturaID)
@Html.ValidationMessageFor(model => model.videoModel.KalturaID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.Size)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.videoModel.Size)
@Html.ValidationMessageFor(model => model.videoModel.Size)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.Date)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.videoModel.Date)
@Html.ValidationMessageFor(model => model.videoModel.Date)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.UploadedBy)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.videoModel.UploadedBy)
@Html.ValidationMessageFor(model => model.videoModel.UploadedBy)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.UserId, "User")
</div>
<div class="editor-field">
@Html.DropDownList("UserId", String.Empty)
@Html.ValidationMessageFor(model => model.videoModel.UserId)
</div>
<div class="editor-field">
<input name="model.vid" type="file" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
當我提交表單時,VM 的 videoModel 部分已填寫,但實際文件為空.有什么想法嗎?
When I submit the form the videoModel part of VM is filled out but vid the actual file is null. Any ideas?
推薦答案
根據OP評論更新
在 web.config 文件中設置最大文件長度改變?"到您希望最大的文件大小,例如 65536 是 64MB
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" />
您的操作不正確.它需要接受文件作為它自己的參數(或者如果多個使用 IEnumerable
作為參數類型)
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);
}
這篇關于ASP MVC 文件上傳 HttpPostedFileBase 為空的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!