Golang simple implementation code to get the upload file size

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.




Package Mainimport ("FMT" "io" "Log" "Net/http" "OS")//Gets the file size of the interface type size interface {size () int64}//Gets the file information of the interface type Stat interface {Stat () (OS. FileInfo, error)}//Hello World, the Web Serverfunc helloserver (w http. Responsewriter, R *http. Request) {if "POST" = = R.method {file, _, Err: = R.formfile ("UserFile") if err! = Nil {http. Error (W, err. Error (), Return}if statinterface, OK: = file. (Stat); OK {FileInfo, _: = Statinterface.stat () fmt. fprintf (W, "Size of uploaded file:%d", fileinfo.size ())}if sizeinterface, OK: = file. (Size); OK {fmt. fprintf (w, "Upload file Size:%d", sizeinterface.size ())}return}//upload page W. Header (). ADD ("Content-type", "text/html") W.writeheader (HTML): = ' <form enctype= ' multipart/form-data ' action= '/hello ' method= "POST" > Send this file: <input name= "UserFile" type= "file"/> <input type= "Submit" value= "Send Fi" Le "/></form> ' io. WriteString (w, HTML)}func Main () {http. Handlefunc ("/hello", HelloServer) Err: = http. Listenandserve (": 12345", nil) if err! = Nil {log. Fatal("Listenandserve:", Err)}} 







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.