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.

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

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?