Can an ASP.NET MVC controller return an Image?

前端 未结 19 1643
旧时难觅i
旧时难觅i 2020-11-22 02:10

Can I create a Controller that simply returns an image asset?

I would like to route this logic through a controller, whenever a URL such as the following is requeste

19条回答
  •  醉话见心
    2020-11-22 02:51

    I also encountered similar requirement,

    So in my case I make a request to Controller with the image folder path, which in return sends back a ImageResult object.

    Following code snippet illustrate the work:

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

    And in the Controller from the the image path I produce the image and return it back to the caller

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

提交回复
热议问题