Gorilla/mux:图片未显示

huangapple go评论88阅读模式
英文:

Gorilla/mux: Image not displaying

问题

我是你的中文翻译助手,以下是你提供的代码的翻译:

我是Golang的新手。我需要显示一张图片。我尝试使用Gorilla/mux库。
但是我仍然得到错误:404。我认为可能是我使用mux库的地方不正确。

主函数:

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

将数据传递给HTML的函数:

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

	// 下面是关于Cookie的部分
	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)
		}
	}
}

在HTML文件中的使用:

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

通过以下代码将{{.Photo}}的值存储到数据库中:

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

所以{{.Photo}}的值将类似于以下内容:

frontend/images/20160202171411.jpg

希望对你有帮助!如果你有任何其他问题,请随时问我。

英文:

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 (
	    &quot;net/http&quot;
	    &quot;mytestsite/handlers&quot;
	     &quot;log&quot;
	     &quot;github.com/gorilla/mux&quot;
     )


    func main() {
	
	r := mux.NewRouter()
	r.HandleFunc(&quot;/register&quot;, handlers.RegisterHandler)
	r.HandleFunc(&quot;/sucess&quot;, handlers.Sucess)
	r.HandleFunc(&quot;/login&quot;, handlers.Login)
	r.HandleFunc(&quot;/list&quot;, handlers.ViewList)
	r.HandleFunc(&quot;/logout&quot;, handlers.Logout)
	r.HandleFunc(&quot;/edit&quot;, handlers.Edit)
	r.HandleFunc(&quot;/EditnList&quot;, handlers.EditnList)
	r.HandleFunc(&quot;/notvalid&quot;, handlers.NotValid)
	r.HandleFunc(&quot;/delete&quot;, handlers.Delete)
	r.HandleFunc(&quot;/listchoose&quot;, handlers.ListChoose)
	r.HandleFunc(&quot;/email&quot;, handlers.SendEmail)
	images := http.StripPrefix(&quot;/images/&quot;, http.FileServer(http.Dir(&quot;./frontend/images/&quot;)))
 	r.PathPrefix(&quot;/images/&quot;).Handler(images)
	  
	if err := http.ListenAndServe(&quot;:8181&quot;, r); err != nil {
		log.Fatal(&quot;http.ListenAndServe: &quot;, 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 != &quot;&quot;){
		err:=GetTemplate(&quot;list&quot;).Execute(w,dbResults)
		if err!=nil{
			panic(err)
		}
	} else { 
		viewModel.ErrorMessage=&quot;Please Enter Email Id and Password..&quot;
		err:=GetTemplate(&quot;login&quot;).Execute(w,viewModel)
		if err!=nil{
			panic(err)
		}
	}
}

Usage in html file

&lt;td&gt;&lt;img src=&quot;{{.Photo}}&quot; alt=&quot;{{.Photo}}&quot; style=&quot;width:50px;height:50px;&quot;/&gt;&lt;/td&gt;

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

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

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

frontend/images/20160202171411.jpg

答案1

得分: 1

你在这里混淆了两个路由器。

首先,http包有一个全局默认处理程序,称为:DefaultServeMux。这是http.ServeMux的一个实例。

当你调用http.HandleFunc时,DefaultServeMux是注册处理程序的地方。

当你调用http.ListenAndServe时,第二个参数是指定端口上的HTTP请求使用的处理程序。

如果你将处理程序设置为nil(就像你在代码中所做的那样),你告诉http服务器使用http.DefaultServeMux。

Gorilla mux是http.ServeMux的一个替代方案。一般来说,你可以使用其中之一。

在你的情况下,你正在使用gorilla mux注册你的文件服务器,但是告诉http.ListenAndServe使用http.DefaultServeMux(通过省略处理程序)。

你可以使用标准http库的默认mux注册你的文件服务器(通过http.Handle),或者更改你的函数映射以使用Gorilla mux注册(通过r.HandlerFunc)。

如果你选择使用Gorilla(更灵活的解决方案,但在你的示例代码中并不是必需的),那么将其传递给ListenAndServe而不是nil。

英文:

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.

huangapple
  • 本文由 发表于 2016年2月3日 15:50:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/35171293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定