英文:
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 (
"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
得分: 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论