douzhang6176 2016-02-03 07:50
浏览 10
已采纳

大猩猩/多路复用器:图像不显示

I am new in Golang. I need display an image. I tried using Gorilla/mux. But I am still getting Error:404. I thing may be the place where I used mux code is not correct.

Main func

    package main
    import (
        "net/http"
        "mytestsite/handlers"
         "log"
         "github.com/gorilla/mux"
     )


    func main() {

    r := mux.NewRouter()
    r.HandleFunc("/register", handlers.RegisterHandler)
    r.HandleFunc("/sucess", handlers.Sucess)
    r.HandleFunc("/login", handlers.Login)
    r.HandleFunc("/list", handlers.ViewList)
    r.HandleFunc("/logout", handlers.Logout)
    r.HandleFunc("/edit", handlers.Edit)
    r.HandleFunc("/EditnList", handlers.EditnList)
    r.HandleFunc("/notvalid", handlers.NotValid)
    r.HandleFunc("/delete", handlers.Delete)
    r.HandleFunc("/listchoose", handlers.ListChoose)
    r.HandleFunc("/email", handlers.SendEmail)
    images := http.StripPrefix("/images/", http.FileServer(http.Dir("./frontend/images/")))
    r.PathPrefix("/images/").Handler(images)

    if err := http.ListenAndServe(":8181", r); err != nil {
        log.Fatal("http.ListenAndServe: ", err)
    }        
}

Func which pass data to html

func ViewList(w http.ResponseWriter, r *http.Request) {
    viewModel:=viewmodels.RegisterViewModel{}
    user:=models.User{}
    dbResults := user.ViewDB() 

    //Cookies below
    cookieCheck := getCookie(r)

    if (cookieCheck != ""){
        err:=GetTemplate("list").Execute(w,dbResults)
        if err!=nil{
            panic(err)
        }
    } else { 
        viewModel.ErrorMessage="Please Enter Email Id and Password.."
        err:=GetTemplate("login").Execute(w,viewModel)
        if err!=nil{
            panic(err)
        }
    }
}

Usage in html file

<td><img src="{{.Photo}}" alt="{{.Photo}}" style="width:50px;height:50px;"/></td>

Value of {{.Photo}} is stored to Db by following code:

ctime := time.Now()
uploadedfile := "frontend/images"+ctime.Format("20060102150405")+".jpg"
out, err := os.Create(uploadedfile)

SO value of {{.Photo}} will be like as follows

frontend/images/20160202171411.jpg
  • 写回答

1条回答 默认 最新

  • dourou9477 2016-02-03 19:07
    关注

    You are mixing two routers here.

    First off, the http package has a package global default handler called: DefaultServeMux. This is an instance of http.ServeMux

    When you call http.HandleFunc, DefaultServeMux is where that handler gets registered (source)

    When you call http.ListenAndServe, the second parameter is what handler to use for HTTP requests that come in on your specified port.

    If you pass nil as the handler (like you do in your code), you are telling the http server to use http.DefaultServeMux

    Gorilla mux is an alternative to http.ServeMux. In general, you use one or the other.

    In your case, you are registering your file server with a gorilla mux, but then telling http.ListenAndServe to use http.DefaultServeMux (by omitting the handler).

    You can either register your file serve with the standard http library's default mux (via: http.Handle) or change your function mappings to register with your Gorilla mux (via: r.HandlerFunc).

    If you choose to use Gorilla (more flexible solution, but is not really necessary given your example code), then pass that instead of nil to ListenAndServe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度