gpt4 book ai didi

c# - ASP MVC 文件上传 HttpPostedFileBase 为空

转载 作者:可可西里 更新时间:2023-11-01 08:05:13 24 4
gpt4 key购买 nike

在我的 Controller 中,因为我希望能够填写有关视频的一些详细信息并实际上传它,Video 类不需要实际视频,因为它将被传递到另一个网络服务。

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 部分已填写,但 vid 实际文件为空。有什么想法吗?

最佳答案

根据OP评论更新

在 web.config 文件中设置最大文件长度改变“?”到你想要的最大文件大小,例如 65536 是 64MB

<configuration>
<system.web>
<httpRuntime maxRequestLength="?" />
</system.web>
</configuration>

您不能将文件添加到模型中,它将位于它自己的字段中,而不是模型的一部分

<input name="videoUpload" type="file" />

您的操作不正确。它需要接受文件作为它自己的参数(或者如果多次使用 IEnumerable<HttpPostedFileBase> 作为参数类型)

[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);
}

如果您允许选择多个文件,则必须允许

[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);
}

关于c# - ASP MVC 文件上传 HttpPostedFileBase 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10856240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com