The example in this article describes how to Golang a simple way to get the uploaded file size. Share to everyone for your reference, specific as follows:
Copy Code code as follows:
Package Main
Import (
"FMT"
"IO"
"Net/http"
"Log"
"OS"
)
interface to get file size
Type Size Interface {
Size () Int64
}
interface to get file information
Type Stat Interface {
Stat () (OS. FileInfo, error)
}
Hello world, the Web server
Func HelloServer (w http. Responsewriter, R *http. Request) {
If "POST" = = R.method {
File, _, Err: = R.formfile ("UserFile")
If Err!= nil {
http. Error (W, err.) Error (), 500)
Return
}
If statinterface, OK: = file. (Stat); OK {
FileInfo, _: = Statinterface.stat ()
Fmt. fprintf (w, "Upload file size is:%d", fileinfo.size ())
}
If sizeinterface, OK: = file. (Size); OK {
Fmt. fprintf (w, "Upload file size is:%d", sizeinterface.size ())
}
Return
}
Upload page
W.header (). ADD ("Content-type", "text/html")
W.writeheader (200)
HTML: = '
<form enctype= "Multipart/form-data" action= "/hello" method= "POST" >
Send This file: <input name= "UserFile" type= "file"/>
<input type= "Submit" value= "Send File"/>
</form>
`
Io. WriteString (w, HTML)
}
Func Main () {
http. Handlefunc ("/hello", HelloServer)
ERR: = http. Listenandserve (": 12345", nil)
If Err!= nil {
Log. Fatal ("Listenandserve:", err)
}
}
I hope this article will help you with your go language program.