我可以创建一个简单地返回图像资产的控制器吗?

我想通过控制器路由这个逻辑,每当请求如下URL时:

www.mywebsite.com/resource/image/topbanner

控制器将查找topbanner.png并将图像直接发送回客户端。

我见过这样的例子,你必须创建一个视图-我不想使用视图。我想只用控制器来做。

这可能吗?


当前回答

稍微解释一下迪兰的回应:

有三个类实现了FileResult类:

System.Web.Mvc.FileResult
      System.Web.Mvc.FileContentResult
      System.Web.Mvc.FilePathResult
      System.Web.Mvc.FileStreamResult

它们都是不言自明的:

For file path downloads where the file exists on disk, use FilePathResult - this is the easiest way and avoids you having to use Streams. For byte[] arrays (akin to Response.BinaryWrite), use FileContentResult. For byte[] arrays where you want the file to download (content-disposition: attachment), use FileStreamResult in a similar way to below, but with a MemoryStream and using GetBuffer(). For Streams use FileStreamResult. It's called a FileStreamResult but it takes a Stream so I'd guess it works with a MemoryStream.

下面是一个使用内容处理技术的例子(未测试):

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult GetFile()
    {
        // No need to dispose the stream, MVC does it for you
        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data", "myimage.png");
        FileStream stream = new FileStream(path, FileMode.Open);
        FileStreamResult result = new FileStreamResult(stream, "image/png");
        result.FileDownloadName = "image.png";
        return result;
    }

其他回答

我有两个选择:

1)实现你自己的IViewEngine,设置控制器的ViewEngine属性,你正在使用你的ImageViewEngine在你想要的“image”方法。

2)使用视图:-)。只需改变内容类型等。

稍微解释一下迪兰的回应:

有三个类实现了FileResult类:

System.Web.Mvc.FileResult
      System.Web.Mvc.FileContentResult
      System.Web.Mvc.FilePathResult
      System.Web.Mvc.FileStreamResult

它们都是不言自明的:

For file path downloads where the file exists on disk, use FilePathResult - this is the easiest way and avoids you having to use Streams. For byte[] arrays (akin to Response.BinaryWrite), use FileContentResult. For byte[] arrays where you want the file to download (content-disposition: attachment), use FileStreamResult in a similar way to below, but with a MemoryStream and using GetBuffer(). For Streams use FileStreamResult. It's called a FileStreamResult but it takes a Stream so I'd guess it works with a MemoryStream.

下面是一个使用内容处理技术的例子(未测试):

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult GetFile()
    {
        // No need to dispose the stream, MVC does it for you
        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data", "myimage.png");
        FileStream stream = new FileStream(path, FileMode.Open);
        FileStreamResult result = new FileStreamResult(stream, "image/png");
        result.FileDownloadName = "image.png";
        return result;
    }

为什么不简单一点,使用波浪号~操作符呢?

public FileResult TopBanner() {
  return File("~/Content/images/topbanner.png", "image/png");
}
if (!System.IO.File.Exists(filePath))
    return SomeHelper.EmptyImageResult(); // preventing JSON GET/POST exception
else
    return new FilePathResult(filePath, contentType);

SomeHelper.EmptyImageResult()应该返回具有现有图像的FileResult(例如1x1透明)。

这是最简单的方法,如果你有文件存储在本地驱动器。 如果文件是字节[]或流-然后使用FileContentResult或FileStreamResult Dylan建议。

我也遇到过类似的要求,

所以在我的例子中,我用图像文件夹路径向Controller发出请求,它返回一个ImageResult对象。

下面的代码片段说明了这项工作:

var src = string.Format("/GenericGrid.mvc/DocumentPreviewImageLink?fullpath={0}&routingId={1}&siteCode={2}", fullFilePath, metaInfo.RoutingId, da.SiteCode);

                if (enlarged)
                    result = "<a class='thumbnail' href='#thumb'>" +
                        "<img src='" + src + "' height='66px' border='0' />" +
                        "<span><img src='" + src + "' /></span>" +
                        "</a>";
                else
                    result = "<span><img src='" + src + "' height='150px' border='0' /></span>";

在控制器中,我从图像路径中生成图像并将它返回给调用者

try
{
  var file = new FileInfo(fullpath);
  if (!file.Exists)
     return string.Empty;


  var image = new WebImage(fullpath);
  return new ImageResult(new MemoryStream(image.GetBytes()), "image/jpg");


}
catch(Exception ex)
{
  return "File Error : "+ex.ToString();
}