跳至主要内容

go handle upload multiple File

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



//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>

评论

此博客中的热门博文

android hide actionbar

public class MainActivity extends Activity { ActionBar actionBar; //声明ActionBar @Override protected void onCreate( Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView( R .layout.activity_main); actionBar = getSupportActionBar(); //得 到ActionBar actionBar.hide(); //隐藏ActionBar } }

go url encoding

func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits. func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.