2014-10-18 92 views
4

我正在编写一个MVC 5 web应用程序来更新博客文章。我希望能够让用户将视频上传到内容文件夹,然后将文件名作为字符串存储在数据库中。不过,我似乎错过了一件重要的作品。如何在ASP.NET MVC 5中上传视频并将视图中的视频文件传递给方法?

我有一个方法来更新除了视频部分正在工作的职位。

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" />导致视频对象在传递到Update方法时为空。

你怎么会在视频文件传递到更新方法与所有其他文本数据的视图,例如dateTimetitletagsbody设置?

+0

添加'form'属性'ENCTYPE = “的multipart/form-data的”',你必须捕捉'视频文件HttpContext.Current.Request.Files' in ActionResult Update()' – 2014-10-18 21:56:20

+0

@Venkata我添加了'enctype',但是如何使用HttpContext捕获视频文件? – 2014-10-18 22:08:41

+0

出于某种原因,我不得不使用'System.Web.HttpContext.Current.Request.Files',它不会通过在类文件的顶部添加'using System.Web'来工作。 – 2014-10-18 22:16:23

回答

2

下面是片段的,我只是在这里输入给一个想法,你能理解,如果你需要更多的信息让我知道

[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"); 
       if (!Directory.Exists(uploadFilesDir)) { 
        Directory.CreateDirectory(uploadFilesDir); 
       } 
       var fileSavePath = Path.Combine(uploadFilesDir, httpPostedFile.FileName); 

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

      } 

      return Content("Uploaded Successfully"); 
    } 
+0

使用'System.Web.HttpContext.Current.Request.Files'工作...我现在有来自视图的文件传入方法 – 2014-10-18 22:29:37

+0

我得到了文件名,但是一旦拥有它,你如何将文件下载到文件夹? – 2014-10-18 22:30:22

+0

好的,用保存动作更新答案 – 2014-10-18 22:31:07

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

我只是在视图中添加enctype属性的form

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

你能否更新你的答案你是如何将视频列入页面的?我将非常感激。需要相同的概念。 – Felixerity 2015-03-24 11:41:14