Directly on the code: Func Upload (w http. Responsewriter, R *http. Request) {
Fmt. Println ("Method:", R.method)//method of obtaining the request
if R.method = = "GET" {
Crutime: = time. Now (). Unix ()
H: = MD5. New ()
Io. WriteString (H, StrConv. Formatint (Crutime, 10))
Token: = FMT. Sprintf ("%x", H.sum (nil))
T, _: = template. Parsefiles ("Upload.gtpl")
T.execute (W, token)
} else {
R.parsemultipartform (<< 20)
File, handler, err: = R.formfile ("UploadFile")
If err! = Nil {
Fmt. PRINTLN (ERR)
Return
}
Defer file. Close ()
Fmt. fprintf (W, "%v", handler. Header)
F, err: = OS. OpenFile ("./test/" +handler. Filename, OS. O_wronly|os. O_create, 0666)
If err! = Nil {
Fmt. PRINTLN (ERR)
Return
}
Defer F.close ()
Io. Copy (f, file)
}
}
Func Main () {
http. Handlefunc ("/", upload)
ERR: = http. Listenandserve (": 9000", nil)
If err! = Nil {
Fmt. Println ("Error:", err)
}
}
UPLOAD.GTPL file as follows
This article from "East God to a dozen five" blog, declined reprint!
Example of Go file upload