Skip to content

Commit 54df3bb

Browse files
author
fangdong
committed
templates
1 parent d4c3976 commit 54df3bb

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

chapter5/photoweb.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ var templates = make(map[string]*template.Template)
2323
func init() {
2424
fileInfoArr, err := ioutil.ReadDir(TEMPLATE_DIR)
2525
check(err)
26-
2726
var templateName, templatePath string
2827

2928
for _, fileInfo := range fileInfoArr {
@@ -66,14 +65,14 @@ func isExists(path string) (bool, error) {
6665

6766
func uploadHandler(w http.ResponseWriter, r *http.Request) {
6867
if r.Method == "GET" {
69-
renderHtml(w, "upload", nil);
68+
renderHtml(w, "upload.html", nil);
7069
}
7170
if r.Method == "POST" {
7271
f, h, err := r.FormFile("image")
7372
check(err)
7473
filename := h.Filename
7574
defer f.Close()
76-
t, err := ioutil.TempFile(UPLOAD_DIR, filename)
75+
t, err := os.Create(UPLOAD_DIR + "/" + filename)
7776
check(err)
7877
defer t.Close()
7978
_, err = io.Copy(t, f)
@@ -104,7 +103,7 @@ func listHandler(w http.ResponseWriter, r *http.Request) {
104103
images = append(images, fileInfo.Name())
105104
}
106105
locals["images"] = images
107-
renderHtml(w, "list", locals)
106+
renderHtml(w, "list.html", locals)
108107
}
109108

110109
func safeHandler(fn http.HandlerFunc) http.HandlerFunc {

chapter5/uploads/airplane.jpg

83.2 KB
Loading

chapter5/uploads/box.png

49.5 KB
Loading

chapter5/views/list.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>List</title>
6+
</head>
7+
<body>
8+
<ol>
9+
{{range $.images}}
10+
<li><a href="/view?id={{.|urlquery}}">{{.|html}}</a></li>
11+
{{end}}
12+
</ol>
13+
</body>
14+
</html>

chapter5/views/upload.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Upload</title>
6+
</head>
7+
<body>
8+
<form method="POST" action="/upload" enctype="multipart/form-data">
9+
Choose an image to upload: <input name = "image" type="file" />
10+
<input type="submit" value="Upload" />
11+
</form>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)