濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > golang簡(jiǎn)單獲取上傳文件大小的實(shí)現(xiàn)代碼

golang簡(jiǎn)單獲取上傳文件大小的實(shí)現(xiàn)代碼

熱門(mén)標(biāo)簽:電梯外呼訪(fǎng)客系統(tǒng) 電銷(xiāo)機(jī)器人可以補(bǔ)救房產(chǎn)中介嗎 谷歌便利店地圖標(biāo)注 成都呼叫中心外呼系統(tǒng)平臺(tái) 騰訊外呼系統(tǒng)價(jià)格 百度地圖標(biāo)注搜索關(guān)鍵詞 ?兓? 最短的地圖標(biāo)注 浙江人工智能外呼管理系統(tǒng)

本文實(shí)例講述了golang簡(jiǎn)單獲取上傳文件大小的方法。分享給大家供大家參考,具體如下:

復(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
    }
    // 上傳頁(yè)面
    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)
    }
}

希望本文所述對(duì)大家Go語(yǔ)言程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • golang實(shí)現(xiàn)的文件上傳下載小工具
  • golang語(yǔ)言實(shí)現(xiàn)的文件上傳與文件下載功能示例
  • Golang實(shí)現(xiàn)異步上傳文件支持進(jìn)度條查詢(xún)的方法
  • Golang+Android基于HttpURLConnection實(shí)現(xiàn)的文件上傳功能示例
  • Golang實(shí)現(xiàn)http文件上傳小功能的案例

標(biāo)簽:眉山 宜昌 雅安 紹興 邢臺(tái) 七臺(tái)河 上海 盤(pán)錦

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《golang簡(jiǎn)單獲取上傳文件大小的實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  golang,簡(jiǎn)單,獲取,上傳,文件,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《golang簡(jiǎn)單獲取上傳文件大小的實(shí)現(xiàn)代碼》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于golang簡(jiǎn)單獲取上傳文件大小的實(shí)現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    南京市| 清河县| 若尔盖县| 文安县| 汾阳市| 科技| 茶陵县| 巧家县| 合川市| 灯塔市| 玉溪市| 阜阳市| 乌兰察布市| 乳山市| 尖扎县| 鹤峰县| 丽江市| 侯马市| 内乡县| 江川县| 盘锦市| 望城县| 寿宁县| 延津县| 延川县| 上林县| 古田县| 中阳县| 体育| 巫山县| 安乡县| 榆社县| 库尔勒市| 罗城| 邵东县| 武冈市| 崇礼县| 威远县| 前郭尔| 朝阳县| 克什克腾旗|