Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John Churchley 272 posts 1258 karma points c-trib
    Mar 26, 2015 @ 16:25
    John Churchley
    0

    Getting Image from database using a Surfacecontroller

    Hi, 

    I trying to get an image contained in a database through a surface controller. Anyone with any ideas? 

    Using Umbraco 7.2.4

    My attempt below:

    View

    <img src="@Url.Action("GetImage", "ImageSurface", new { id = item.Image_ID })"  />
    

    Controller

    public class ImageSurfaceController : Umbraco.Web.Mvc.SurfaceController
        {
            //
            // GET: /ImageSurface/
    
            public ActionResult GetImage(int id)
            {
                var db = Database.Open("databaseName");
                var sql = "SELECT Image, Image_ID " +
                          "FROM tbl_Image " +
                          "WHERE Image_ID = '" + id + "'";
    
                var imageData = db.QuerySingle(sql);
    
                return File(imageData, "image/gif");
            }
    
        }
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 26, 2015 @ 18:22
    Jeroen Breuer
    0

    Hello,

    When you debug does your controller get hit? I'm not sure if Url.Action works with a SurfaceController.

    You could try route hijacking. Than you do your logic in the controller, update the model and pass that to the view. I have many route hijacking examples in the Hybrid Framework.

    Jeroen

  • John Churchley 272 posts 1258 karma points c-trib
    Mar 26, 2015 @ 18:23
    John Churchley
    0

    Thanks Jeroen looks very useful! Is there any other way you would suggest?

  • John Churchley 272 posts 1258 karma points c-trib
    Mar 26, 2015 @ 18:56
    John Churchley
    0

    Hi Jeroen,

    The reason I'm trying to use a controller is because the original way someone programmed in (6.1.6) no longer works do you have any ideas why this could be? 

    Now using 7.2.4

    Image reference

    <imgsrc="/[email protected]_ID"style="width:150px; height:auto;"/>

    GetImages Razor

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{

            var id = Request["id"];

            var db = Database.Open("www.???.com");

            var sql = "SELECT Image, Image_ID "+

                      "FROM tbl_Image "+

                      "WHERE Image_ID = '"+id+"'";

            var file = db.QuerySingle(sql);

            Response.ContentType = "image/gif";

            Response.BinaryWrite((byte[])file.Image);

            Response.Cache.SetLastModified(DateTime.Now.AddYears(-1));

    }

    Thanks

  • John Churchley 272 posts 1258 karma points c-trib
    Mar 27, 2015 @ 10:47
    John Churchley
    0

    For anyone interested in the previous post, I forgot to create a document type with of the name GetImages which is why it wasn't working.

Please Sign in or register to post replies

Write your reply to:

Draft