Go語言實(shí)現(xiàn)文件上傳
本文實(shí)例為大家分享了Go實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
文件上傳:客戶端把上傳文件轉(zhuǎn)換為二進(jìn)制流后發(fā)送給服務(wù)器,服務(wù)器對二進(jìn)制流進(jìn)行解析
HTML表單(form)enctype(Encode Type)屬性控制表單在提交數(shù)據(jù)到服務(wù)器時(shí)數(shù)據(jù)的編碼類型.
enctype=”application/x-www-form-urlencoded” 默認(rèn)值,表單數(shù)據(jù)會被編碼為名稱/值形式
- enctype=”multipart/form-data” 編碼成消息,每個(gè)控件對應(yīng)消息的一部分.請求方式必須是post
- enctype=”text/plain” 純文本形式進(jìn)行編碼的
HTML模版內(nèi)容如下(在項(xiàng)目/view/index.html)
<!DOCTYPE html> <html lang="en"> <head> ? ? <meta charset="UTF-8"> ? ? <title>文件上傳</title> </head> <body> <form action="upload" enctype="multipart/form-data" method="post"> ? ? 用戶名:<input type="text" name="username"/><br/> ? ? 密碼:<input type="file" name="photo"/><br/> ? ? <input type="submit" value="注冊"/> </form> </body> </html>
服務(wù)端可以使用FormFIle(“name”)獲取上傳到的文件,官方定義如下
// FormFile returns the first file for the provided form key. // FormFile calls ParseMultipartForm and ParseForm if necessary. func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) { ?? ?if r.MultipartForm == multipartByReader { ?? ??? ?return nil, nil, errors.New("http: multipart handled by MultipartReader") ?? ?} ?? ?if r.MultipartForm == nil { ?? ??? ?err := r.ParseMultipartForm(defaultMaxMemory) ?? ??? ?if err != nil { ?? ??? ??? ?return nil, nil, err ?? ??? ?} ?? ?} ?? ?if r.MultipartForm != nil && r.MultipartForm.File != nil { ?? ??? ?if fhs := r.MultipartForm.File[key]; len(fhs) > 0 { ?? ??? ??? ?f, err := fhs[0].Open() ?? ??? ??? ?return f, fhs[0], err ?? ??? ?} ?? ?} ?? ?return nil, nil, ErrMissingFile }
multipart.File是文件對象
// File is an interface to access the file part of a multipart message. // Its contents may be either stored in memory or on disk. // If stored on disk, the File's underlying concrete type will be an *os.File. type File interface { ?? ?io.Reader ?? ?io.ReaderAt ?? ?io.Seeker ?? ?io.Closer }
封裝了文件的基本信息
// A FileHeader describes a file part of a multipart request. type FileHeader struct { ?? ?Filename string?? ??? ??? ??? ??? ?//文件名 ?? ?Header ? textproto.MIMEHeader?? ?//MIME信息 ?? ?Size ? ? int64?? ??? ??? ??? ??? ?//文件大小,單位bit ?? ?content []byte?? ??? ??? ??? ??? ?//文件內(nèi)容,類型[]byte ?? ?tmpfile string?? ??? ??? ??? ??? ?//臨時(shí)文件 }
服務(wù)器端編寫代碼如下
- 獲取客戶端傳遞后的文件流,把文件保存到服務(wù)器即可
package main import ( ? ?"net/http" ? ?"fmt" ? ?"html/template" ? ?"io/ioutil" ) /* 顯示歡迎頁upload.html ?*/ func welcome(rw http.ResponseWriter, r *http.Request) { ? ?t, _ := template.ParseFiles("template/html/upload.html") ? ?t.Execute(rw, nil) } /* 文件上傳 ?*/ func upload(rw http.ResponseWriter, r *http.Request) { ? ?//獲取普通表單數(shù)據(jù) ? ?username := r.FormValue("username") ? ?fmt.Println(username) ? ?//獲取文件流,第三個(gè)返回值是錯(cuò)誤對象 ? ?file, header, _ := r.FormFile("photo") ? ?//讀取文件流為[]byte ? ?b, _ := ioutil.ReadAll(file) ? ?//把文件保存到指定位置 ? ?ioutil.WriteFile("D:/new.png", b, 0777) ? ?//輸出上傳時(shí)文件名 ? ?fmt.Println("上傳文件名:", header.Filename) } func main() { ? ?server := http.Server{Addr: "localhost:8899"} ? ?http.HandleFunc("/", welcome) ? ?http.HandleFunc("/upload", upload) ? ?server.ListenAndServe() }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- GO語言原生實(shí)現(xiàn)文件上傳功能
- GO實(shí)現(xiàn)文件上傳操作
- Go實(shí)現(xiàn)文件分片上傳
- Go Gin實(shí)現(xiàn)文件上傳下載的示例代碼
- golang實(shí)現(xiàn)ftp實(shí)時(shí)傳輸文件的案例
- Golang實(shí)現(xiàn)異步上傳文件支持進(jìn)度條查詢的方法
- golang socket斷點(diǎn)續(xù)傳大文件的實(shí)現(xiàn)方法
- 用go gin server來做文件上傳服務(wù)
- Golang+Android基于HttpURLConnection實(shí)現(xiàn)的文件上傳功能示例
- Golang實(shí)現(xiàn)文件傳輸功能
相關(guān)文章
Golang 實(shí)現(xiàn)簡單隨機(jī)負(fù)載均衡
均衡算法又分為 隨機(jī),輪詢,加權(quán)輪詢,哈希,而隨機(jī)負(fù)載均衡算法就是本文的重點(diǎn),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06Go?io/fs.FileMode文件系統(tǒng)基本操作和權(quán)限管理深入理解
這篇文章主要為大家介紹了Go?io/fs.FileMode文件系統(tǒng)基本操作和權(quán)限管理深入理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01golang時(shí)間/時(shí)間戳的獲取與轉(zhuǎn)換實(shí)例代碼
說實(shí)話,golang的時(shí)間轉(zhuǎn)化還是很麻煩的,最起碼比php麻煩很多,下面這篇文章主要給大家介紹了關(guān)于golang時(shí)間/時(shí)間戳的獲取與轉(zhuǎn)換的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11Golang?Compare?And?Swap算法詳細(xì)介紹
CAS算法是一種有名的無鎖算法。無鎖編程,即不使用鎖的情況下實(shí)現(xiàn)多線程之間的變量同步,也就是在沒有線程被阻塞的情況下實(shí)現(xiàn)變量的同步,所以也叫非阻塞同步Non-blocking?Synchronization2022-10-10Golang?Gin框架獲取請求參數(shù)的幾種常見方式
在我們平常添加路由處理函數(shù)之后,就可以在路由處理函數(shù)中編寫業(yè)務(wù)處理代碼了,但在此之前我們往往需要獲取請求參數(shù),本文就詳細(xì)的講解下gin獲取請求參數(shù)常見的幾種方式,需要的朋友可以參考下2024-02-02Go語言開源庫實(shí)現(xiàn)Onvif協(xié)議客戶端設(shè)備搜索
這篇文章主要為大家介紹了Go語言O(shè)nvif協(xié)議客戶端設(shè)備搜索示例實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04Go語言fmt庫詳解與應(yīng)用實(shí)例(格式化輸入輸出功能)
fmt庫是Go語言中一個(gè)強(qiáng)大而靈活的庫,提供了豐富的格式化輸入輸出功能,通過本文的介紹和實(shí)例演示,相信你對fmt庫的使用有了更深的理解,感興趣的朋友一起看看吧2023-10-10