package main
import (
f "fmt"
"html/template"
"io"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", home)
http.HandleFunc("/uploadfiles", uploadfiles)
http.ListenAndServe(":8080", nil)
}
func home(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("index.html")
t.Execute(w, nil)
}
func uploadfiles(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(1 << 30)
fhs := r.MultipartForm.File["uploadFile"]
for _, v := range fhs {
file, err := v.Open()
if err != nil {
f.Println(err)
return
}
mf, err := os.OpenFile("./"+v.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
f.Println(err)
return
}
defer mf.Close()
io.Copy(mf, file)
}
f.Fprintf(w, "ok")
}
import (
f "fmt"
"html/template"
"io"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", home)
http.HandleFunc("/uploadfiles", uploadfiles)
http.ListenAndServe(":8080", nil)
}
func home(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("index.html")
t.Execute(w, nil)
}
func uploadfiles(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(1 << 30)
fhs := r.MultipartForm.File["uploadFile"]
for _, v := range fhs {
file, err := v.Open()
if err != nil {
f.Println(err)
return
}
mf, err := os.OpenFile("./"+v.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
f.Println(err)
return
}
defer mf.Close()
io.Copy(mf, file)
}
f.Fprintf(w, "ok")
}
//index.html
<!DOCTYPE html>
<html>
<head>
<title>upload</title>
</head>
<body>
<form action="/uploadFile" method="post" enctype="multipart/form-data" multiple>
<input type="file" name="uploadFile">
<input type="submit" name="submit">
</form>
</body>
</html>
评论
发表评论