【问题标题】:How to upload video in ASP.NET MVC 5 and pass the video file from a view to a method?如何在 ASP.NET MVC 5 中上传视频并将视频文件从视图传递给方法?
【发布时间】:2019-01-28 11:07:54
【问题描述】:

我正在编写一个 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>

使用&lt;input type="file" name="video" /&gt; 会导致视频对象在传递到Update 方法时为空。

如何将视频文件与 View 中的所有其他文本数据集(例如 dateTimetitletagsbody)一起传递给 Update 方法?

【问题讨论】:

  • 添加form属性enctype="multipart/form-data",你必须在ActionResult Update()中捕获带有HttpContext.Current.Request.Files的视频文件
  • @Venkata 我添加了enctype,但是如何使用 HttpContext 捕获视频文件?
  • 由于某种原因,我不得不使用System.Web.HttpContext.Current.Request.Files,在类文件的顶部添加using System.Web 是行不通的。
  • 你可以用Request.Files我给你加sn-p

标签: c# asp.net-mvc


【解决方案1】:

下面是sn-p,我只是在这里输入一个想法,您可以理解,如果您需要更多信息,请告诉我

    [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 工作...我现在将视图中的文件传递给方法
  • 我得到了文件名,但是一旦你有了文件如何下载到文件夹中?
  • 如果我能多次为你投票,我会的,谢谢!
  • 我已经为这个 Qn stackoverflow.com/questions/26354947/… 添加了类似的答案,但它与 web-api 几乎相似,除了控制器,它可能会有所帮助我已经添加了关于如何异步提交的完整测试用例文件。
【解决方案2】:

接受的答案解决了将视频文件从视图中获取到方法中的问题。我只是在我的代码中发布我所做的更改,以防它对任何人有所帮助。

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

我刚刚将enctype 属性添加到视图中的form

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

【讨论】:

  • 能否请您更新您的回答,您是如何将视频列出到页面上的。我将不胜感激。需要相同的概念。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
相关资源
最近更新 更多