Add image from file on local server

64 views
Skip to first unread message

Tony Falcone

unread,
Mar 29, 2017, 8:49:29 AM3/29/17
to gowebuitoolkit
This should be easy to do, but for some reason I am having trouble. I want to load an image (png) that resides on the same host that is running the (gowut) server. Since NewImage() expects a url, I have encoded this as "file:///path_to_file"; however, the image does not display, even though when I view the source, I can see the correct url in the src tag of the img element. (And, when I cut and paste the "file:///path_to_file" in a new browser window's address bar, the image does display.)

What am I missing? Any help would be appreciated.

Thanks,

Tony

Glen Newton

unread,
Mar 29, 2017, 9:17:27 AM3/29/17
to Tony Falcone, gowebuitoolkit
It should not be encoded as "file://..."
From the docs: https://godoc.org/github.com/icza/gowut/gwu#Server
  // Then request for absolute path "/appname/img/faces/happy.gif" will serve
  // "/tmp/myimg/faces/happy.gif", just as the the request for relative path "img/faces/happy.gif".

Remember the browser is interpreting the image location, and it needs to point to an http endpoint to get the image. The endpoint is your gowut app serving the image.

Glen

Tony Falcone

unread,
Mar 29, 2017, 1:35:49 PM3/29/17
to gowebuitoolkit, tony...@gmail.com
Thanks. I am a little embarrassed for not having found that myself; it is very useful (obviously) to be able to bind locations on the server in that manner.

It is in fact so useful (and necessary when wanting to provide content that resides on the server host) that I am wondering if I missed it in any of the examples: I used these to guide me.

Thanks again,

Tony

András Belicza

unread,
Mar 29, 2017, 3:57:50 PM3/29/17
to Tony Falcone, gowebuitoolkit
As Glen noted, you may use Server.AddStaticDir() to "register" a folder of yours whose content will be served by the Gowut server, and so you can use relative paths to refer to files in them, including images.
Message has been deleted

András Belicza

unread,
Feb 15, 2018, 8:30:08 AM2/15/18
to Акашкин Денис, gowebuitoolkit
What is the absolute path of logo.gif? Is it "/home/adm/go/src/adm/gups/pict/logo.gif"? If so, "/UPS/pic/logo.gif" should work.

Note that there is an additional letter "t" at the end of the path you register:

    server.AddStaticDir("pic", "/home/adm/go/src/adm/gups/pict")

If it was not intentional, that might cause the trouble. and this should fix it:

    server.AddStaticDir("pic", "/home/adm/go/src/adm/gups/pic")


Regards,
Andras

On Thu, Feb 15, 2018 at 2:21 PM, Акашкин Денис <akaa...@gmail.com> wrote:
But, may be anyone had the working code sample? I use AddStaticDir func this (wrong) way:

   
    server := gwu.NewServer("UPS", str_localhost_port)
    server
.SetText("* ИБП Титан *")
    server
.AddStaticDir("pic", "/home/adm/go/src/adm/gups/pict")

    img
:= gwu.NewImage("PSDlogo", "pic/logo.gif") // not works
    //img := gwu.NewImage("PSDlogo", "/UPS/pic/logo.gif") // doesn't work
    //img := gwu.NewImage("PSDlogo", "192.168.0.20/UPS/pic/logo.gif") // doesn't work too
    //img := gwu.NewImage("PSDlogo", "http://localhost/UPS/pic/logo.gif") // doesn't work also
    img
.Style().SetSizePx(275, 95)
    win
.Add(img)


logo.gif is inside /home/adm/go/src/adm/gups/pict ,
but browser does not render this image .


среда, 29 марта 2017 г., 15:49:29 UTC+3 пользователь Tony Falcone написал:

Акашкин Денис

unread,
Feb 15, 2018, 8:36:10 AM2/15/18
to gowebuitoolkit
Оh, Im sorry, all works. Full code fragment:

   
server := gwu.NewServer("UPS", str_localhost_port)

    server
.AddStaticDir("pic", "/home/adm/go/src/adm/gups/pict")


   
// Create and build a window
    win
:= gwu.NewWindow("main", "UPS info")
    win
.Style().SetFullWidth()
    win
.SetHAlign(gwu.HACenter)
    win
.SetCellPadding(2)

    img
:= gwu.NewImage("PSDlogo", "pic/logo.png")
    img
.Style().SetSizePx(275, 95)
    win
.Add(img)
 
//......
    server
.AddWin(win)
    server
.Start("main")



My mistake was that I..  foolish stupid. Sorry)


Акашкин Денис

unread,
Feb 15, 2018, 2:15:18 PM2/15/18
to András Belicza, gowebuitoolkit
Yes, it works. 

16 февр. 2018 г. 1:30 пользователь "András Belicza" <icz...@gmail.com> написал:
Reply all
Reply to author
Forward
0 new messages