golang簡單獲取上傳文件大小的實現(xiàn)代碼
更新時間:2016年07月22日 10:59:05 作者:dotcoo
這篇文章主要介紹了golang簡單獲取上傳文件大小的方法,涉及Go語言文件傳輸及文件屬性操作的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了golang簡單獲取上傳文件大小的方法。分享給大家供大家參考,具體如下:
復(fù)制代碼 代碼如下:
package main
import (
"fmt"
"io"
"net/http"
"log"
"os"
)
// 獲取文件大小的接口
type Size interface {
Size() int64
}
// 獲取文件信息的接口
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, "上傳文件的大小為: %d", fileInfo.Size())
}
if sizeInterface, ok := file.(Size); ok {
fmt.Fprintf(w, "上傳文件的大小為: %d", sizeInterface.Size())
}
return
}
// 上傳頁面
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)
}
}
import (
"fmt"
"io"
"net/http"
"log"
"os"
)
// 獲取文件大小的接口
type Size interface {
Size() int64
}
// 獲取文件信息的接口
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, "上傳文件的大小為: %d", fileInfo.Size())
}
if sizeInterface, ok := file.(Size); ok {
fmt.Fprintf(w, "上傳文件的大小為: %d", sizeInterface.Size())
}
return
}
// 上傳頁面
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)
}
}
希望本文所述對大家Go語言程序設(shè)計有所幫助。
您可能感興趣的文章:
- golang?gin框架實現(xiàn)大文件的流式上傳功能
- Golang實現(xiàn)圖片上傳功能的示例代碼
- golang?Gin上傳文件返回前端及中間件實現(xiàn)示例
- Golang 如何限制木馬圖片上傳服務(wù)器的實例
- Golang實現(xiàn)http文件上傳小功能的案例
- golang實現(xiàn)的文件上傳下載小工具
- golang語言實現(xiàn)的文件上傳與文件下載功能示例
- Golang實現(xiàn)異步上傳文件支持進度條查詢的方法
- Golang+Android基于HttpURLConnection實現(xiàn)的文件上傳功能示例
- golang實現(xiàn)文件上傳并轉(zhuǎn)存數(shù)據(jù)庫功能
相關(guān)文章
Go如何優(yōu)雅的關(guān)閉goroutine協(xié)程
本文將介紹首先為什么需要主動關(guān)閉goroutine,并介紹如何在Go語言中關(guān)閉goroutine的常見套路,包括傳遞終止信號和協(xié)程內(nèi)部捕捉終止信號,之后,文章列舉了需要主動關(guān)閉協(xié)程運行的常見場景,希望通過本文的介紹,讀者能夠掌握如何在適當(dāng)?shù)臅r候關(guān)閉goroutine2023-05-05簡單談?wù)凣olang中的字符串與字節(jié)數(shù)組
這篇文章主要給大家介紹了關(guān)于Golang中字符串與字節(jié)數(shù)組的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Golang具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Go語言中make和new函數(shù)的用法與區(qū)別
這篇文章介紹了Go語言中make和new函數(shù)的用法與區(qū)別,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07一百行Golang代碼實現(xiàn)簡單并發(fā)聊天室
這篇文章主要為大家詳細(xì)介紹了一百行Golang代碼如何實現(xiàn)簡單并發(fā)聊天室,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08Golang實現(xiàn)的聊天程序服務(wù)端和客戶端代碼分享
這篇文章主要介紹了Golang實現(xiàn)的聊天程序服務(wù)端和客戶端代碼分享,本文先是講解了實現(xiàn)邏輯,然后給出了實現(xiàn)代碼,需要的朋友可以參考下2014-10-10