如何在ASP.NET MVC 5中上传视频并从视图传递视频文件到方法?

4
我正在编写一个MVC 5 Web应用程序来更新博客文章。我想让用户上传视频到内容文件夹,然后将文件名以字符串形式存储在数据库中。但是,我似乎缺少一个关键部分。
我有一个更新帖子的方法,除了视频部分之外都有效。
public ActionResult Update(int? id, string title, string body, DateTime dateTime, string tags, Video video)
{
    if (!IsAdmin)
    {
        return RedirectToAction("Index");
    }

    var post = GetPost(id); // get the post object

    post.Title = title;
    post.Body = body;
    post.DateTime = dateTime;
    post.Tags.Clear();
    post.VideoFileName = UploadVideo(video);

我已经创建了一个视频类,其中有一个属性。
public class Video
{
    public HttpPostedFileBase File { get; set; }
}

接下来,在与 Update 方法同一类中编写一个方法,用于上传视频并返回文件名。

[HttpPost]
public string UploadVideo(Video video)
{
    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("~/Content/Videos"), fileName);
    video.File.SaveAs(path);

    return fileName;
}

我有一个用于更新方法的视图,但我不知道如何将视频对象从此视图传递到更新方法中,以便我可以将其传递给UploadVideo方法。

<form action="@Href("~/Posts/Update")" method="post" id="postForm">
@if(Model.Id != -1)
{
    <input type="hidden" name="id" value="@Model.Id"/>
}
    @{ var dateTime = Model.DateTime.Year > 2000 ? Model.DateTime : DateTime.Now; }
    <input type="text" name="dateTime" value="@dateTime "/> Date<br />
    <input type="text" name="title" value="@Model.Title " /> Title<br />
    <input type="text" name="tags" value="@ViewBag.Tags " /> Tags<br />
    <textarea name="body" rows="10" cols="80">@Model.Body</textarea><br />
    <br/>
    <br/>
    <input type="file" name="video" />
    <br/>
    <br/>
    <input type="submit" name="submit" value="Submit" />
</form>

使用 <input type="file" name="video" /> 会导致在传递到更新方法时,视频对象为空。
您如何将视频文件与在视图中设置的所有其他文本数据(例如 dateTime,title,tagsbody)一起传递到更新方法中?

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Koti Panga
@Venkata 我已经添加了 enctype,但是您如何使用 HttpContext 捕获视频文件? - Jonathan Kittell
由于某些原因,我不得不使用 System.Web.HttpContext.Current.Request.Files,在类文件顶部添加 using System.Web 无法正常工作。 - Jonathan Kittell
你可以使用 Request.Files,我为你添加了代码片段。 - Koti Panga
2个回答

3
以下是代码片段,我只是在这里输入以给出一个想法,如果您需要更多信息,请告诉我。
    [HttpPost]
    public ActionResult UploadFile()
    {
           var httpPostedFile = Request.Files[0];
           if (httpPostedFile != null) {

                // Validate the uploaded file if you want like content length(optional)

                // Get the complete file path
                var uploadFilesDir = System.Web.HttpContext.Current.Server.MapPath("~/Content/Videos");
                Directory.CreateDirectory(uploadFilesDir);

                var fileSavePath = Path.Combine(uploadFilesDir, httpPostedFile.FileName);

                // Save the uploaded file to "UploadedFiles" folder
                httpPostedFile.SaveAs(fileSavePath);

            }

            return Content("Uploaded Successfully");
    }

使用 System.Web.HttpContext.Current.Request.Files 工作了...现在我已经从视图传递到方法中的文件。 - Jonathan Kittell
我已经得到了文件名,但是一旦拥有它,如何将文件下载到文件夹中呢? - Jonathan Kittell
如果我可以给你点赞超过一次,我一定会的,谢谢! - Jonathan Kittell
我已经为这个问题添加了类似的答案,链接是http://stackoverflow.com/questions/26354947/how-to-get-a-response-status-while-we-submit-a-form-to-web-api/26431680#26431680,但它是针对Web API的,除了控制器之外几乎相同,这可能会有所帮助。我已经添加了完整的测试用例,演示如何异步提交文件。 - Koti Panga

1

接受的答案解决了从视图中获取视频文件并传递到方法中的问题。我在这里发布我在代码中所做的更改,以帮助任何需要的人。

[HttpPost]
public string UploadVideo(HttpFileCollection video)
{
    if (video.Count <= 0) return null;
    var fileName = Path.GetFileName(video.Get(0).FileName);
    var path = Path.Combine(Server.MapPath("~/Content/Videos"), fileName);
    // save video here
    return fileName;
}

[ValidateInput(false)]
public ActionResult Update(int? id, string title, string body, DateTime dateTime, string tags)
{
    if (!IsAdmin)
    {
        return RedirectToAction("Index");
    }

    var post = GetPost(id); // get the post object

    var video = System.Web.HttpContext.Current.Request.Files;

    post.Title = title;
    post.Body = body;
    post.DateTime = dateTime;
    post.Tags.Clear();
    post.VideoFileName = UploadVideo(video);
    // continued, more code
}

public class Video
{
    public HttpFileCollection File { get; set; }
}

我刚在视图中的 form 中添加了 enctype 属性。

<form action="@Href("~/Posts/Update")" method="post" id="postForm" enctype="multipart/form-data">

你能否更新一下你的答案,告诉我如何将视频列表放到页面上?我会非常感激。需要相同的概念。 - Reshav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接