我想在控制器动作的视图页面上显示图像

时间:2014-04-24 08:00:01

标签: c# asp.net-mvc-3 razor

public ActionResult GetContentFromFTP()
{
    string[] FTP_Imagery = Directory.GetFiles(@"F:\abc\imgfiles\Imagery\");
    string[] FTP_Audio = Directory.GetFiles(@"F:\abc\audiofiles\Audio\");

    return View("showfiles");
}

这是我的控制器操作,其中两个字符串数组包含所有音频文件和图像文件的路径。现在,我必须在我的视图页面上显示这些图像" showfiles"。请建议我如何实现这个目标?

4 个答案:

答案 0 :(得分:3)

有多种方法可以将数据从Controller传输到View,您可以使用ViewBag

<强>控制器:

public ActionResult GetContentFromFTP()
{
    string[] FTP_Imagery = Directory.GetFiles(@"F:\abc\imgfiles\Imagery\");
    string[] FTP_Audio = Directory.GetFiles(@"F:\abc\audiofiles\Audio\");
    ViewBag.Add("FTP_Imagery", FTP_Imagery);
    ViewBag.Add("FTP_Audio", FTP_Audio);
    return View("showfiles");
}

查看:

@foreach(var img in ViewBag["FTP_Imagery"]){
    <!-- HTML for each image file -->
}

@foreach(var audio in ViewBag["FTP_Audio"]){
    <!-- HTML for each audio file -->
}

在我看来,你应该利用MVC的强类型视图,并拥有一个ViewModel类(例如ShowFilesViewModel),它具有您的视图所需的任何属性,例如每个阵列 然后强烈地将View输入到该视图模型类:

<强>视图模型:

public class ShowFilesViewModel()
{
    public string[] Images {get; set;}
    public string[] Audio {get; set;}
    // anything else you need for your view...
}

<强>控制器:

public ActionResult GetContentFromFTP()
{
    string[] FTP_Imagery = Directory.GetFiles(@"F:\abc\imgfiles\Imagery\");
    string[] FTP_Audio = Directory.GetFiles(@"F:\abc\audiofiles\Audio\");

    var viewModel = new ShowFilesViewModel() 
    {
        Images = FTP_Imagery;
        Audio = FTP_Audio;
    };

    return View("showfiles", viewModel);
}

查看:

@model ShowFilesViewModel

@foreach(var image in Model.Images){
    <!-- HTML for an image file -->
}
@foreach(var image in Model.Audio){
    <!-- HTML for an audio file -->
}

答案 1 :(得分:1)

Controller.View method有一个重载,它也可以将模型传递给视图,在本例中是你的字符串数组。将您的代码更改为:

public ActionResult GetContentFromFTP()
{
    string[] FTP_Imagery = Directory.GetFiles(@"F:\abc\imgfiles\Imagery\");
    string[] FTP_Audio = Directory.GetFiles(@"F:\abc\audiofiles\Audio\");

    return View("showfiles", FTP_Imagery);
}

在您的视图中,使用模型对象来获取图像并使用标准HTML显示它们。一种解决方案可能如下所示:

@foreach (var image in Model)
{
    <img src="@image" />
    <br />
}

可以在ASP.NET site - Intro to MVCAccessing model from controller找到关于模型和控制器与视图之间通信的好教程。

答案 2 :(得分:1)

你可以创建一个viewmodel类,包含像这样的媒体和图像数组:

class ViewModel{
   Public string[] Images {get;set;}
   Public string[] Videos {get;set;}
}

所以在你的行动中:

public ActionResult GetContentFromFTP()
{
string[] FTP_Imagery = Directory.GetFiles(@"F:\abc\imgfiles\Imagery\");
string[] FTP_Audio = Directory.GetFiles(@"F:\abc\audiofiles\Audio\");
ViewModel vm = new ViewModel(){Images = FTP_Imagery,Videos=FTP_Audio };

return View("showfiles", vm);
}

视图:

@foreach (var image in Model.Images)
{
   <img src="@image" />
   <br />
}
@foreach (var video in Model.Videos)
{
   <video src="@video " />
   <br />
}

答案 3 :(得分:1)

在MVC中播放视频并通过自定义动作过滤器从控制器获取此视频可能对您非常有用。

1)添加新的类文件&#39; VideoDataResult.cs&#39;用于自定义操作过滤器

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Hosting;
    using System.Web.Mvc;

    namespace PlayVideoInMVC.CustomDataResult
    {
        public class VideoDataResult : ActionResult
        {
    /// <summary>
    /// The below method will respond with the Video file
    /// </summary>
    /// <param name="context"></param>
    public override void ExecuteResult(ControllerContext context)
    {

        var strVideoFilePath = HostingEnvironment.MapPath("~/VideoFiles/Test2.mp4");

        context.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=Test2.mp4");

        var objFile = new FileInfo(strVideoFilePath);

        var stream = objFile.OpenRead();
        var objBytes = new byte[stream.Length];
        stream.Read(objBytes, 0, (int)objFile.Length);
        context.HttpContext.Response.BinaryWrite(objBytes);

            } 
        }
    }

2添加&#39; VideoDataController&#39;控制器文件 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用PlayVideoInMVC.CustomDataResult; 使用System.Web.Mvc;

    namespace PlayVideoInMVC.Controllers
    {
        public class VideoDataController : Controller
        {
            //
            // GET: /VideoData/

            public ActionResult Index()
            {
                return new VideoDataResult();
            }

        }
    }

3)添加&#39;查看&#39;的HTML标记。 :         @ {             ViewBag.Title =&#34;索引&#34 ;;         }

    <h2>Play Video</h2>
    <h3><a href="@Url.Action("Index", "VideoData")">Download Video</a> </h3>
    <video width="320" height="240" controls autoplay="autoplay">
        <source src="@Url.Action("Index", "VideoData")" type="video/mp4">
    </video>