簡體   English   中英

如何在ASP.NET MVC 5中上傳視頻並將視頻文件從視圖傳遞到方法?

[英]How to upload video in ASP.NET MVC 5 and pass the video file from a view to a method?

我正在編寫一個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;
}

然后我有一個View for the Update方法,但我不知道如何將視圖對象從此View中導入Update方法,以便我可以將它傳遞給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方法時為null。

如何將視頻文件傳遞給Update方法,並在視圖中設置所有其他文本數據,例如dateTimetitletagsbody

下面是片段,我只是在這里輸入一個想法,你可以理解,如果你需要更多信息,請告訴我

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

接受的答案解決了將視頻文件從視圖中獲取到方法中的問題。 我只是發布了我在代碼中更改的內容,以防它幫助任何人。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM