How to upload video to ASP.NET MVC 5 and transfer a video file from a view to a method?

I am writing an MVC 5 web application to update blog posts. I want the user to be able to upload the video to the content folder and then save the file name as a string in the database. However, I seem to be missing one important thing.

I have a way to update messages that work, except for part of the video.

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

I created a class for videos with one property.

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

Then the method in the same class as the method Updatefor loading the video and returning the file name.

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

Then I have a View for Update method, but I donโ€™t know how to get a video object from this view into the Update method to pass it to the method 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" /> , null Update.

Update , , dateTime, title, tags body?

+4
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");
                Directory.CreateDirectory(uploadFilesDir);

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

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

            }

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

. , , - .

[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">
+1

All Articles