Go語(yǔ)言實(shí)現(xiàn)文件上傳
本文實(shí)例為大家分享了Go實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
文件上傳:客戶端把上傳文件轉(zhuǎn)換為二進(jìn)制流后發(fā)送給服務(wù)器,服務(wù)器對(duì)二進(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ù)會(huì)被編碼為名稱/值形式
- enctype=”multipart/form-data” 編碼成消息,每個(gè)控件對(duì)應(yīng)消息的一部分.請(qǐ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="注冊(cè)"/> </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是文件對(duì)象
// 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ù)器端編寫(xiě)代碼如下
- 獲取客戶端傳遞后的文件流,把文件保存到服務(wù)器即可
package main
import (
? ?"net/http"
? ?"fmt"
? ?"html/template"
? ?"io/ioutil"
)
/*
顯示歡迎頁(yè)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ò)誤對(duì)象
? ?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()
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- GO語(yǔ)言原生實(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來(lái)做文件上傳服務(wù)
- Golang+Android基于HttpURLConnection實(shí)現(xiàn)的文件上傳功能示例
- Golang實(shí)現(xiàn)文件傳輸功能
相關(guān)文章
Go中Gzip與json搭配實(shí)現(xiàn)數(shù)據(jù)壓縮demo
這篇文章主要為大家介紹了Go中Gzip與json搭配使用壓縮數(shù)據(jù)的實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
golang中連接mysql數(shù)據(jù)庫(kù)
這篇文章主要介紹了golang中連接mysql數(shù)據(jù)庫(kù)的步驟,幫助大家更好的理解和學(xué)習(xí)go語(yǔ)言,感興趣的朋友可以了解下2020-12-12
Go語(yǔ)言讀取,設(shè)置Cookie及設(shè)置cookie過(guò)期方法詳解
這篇文章主要介紹了Go語(yǔ)言讀取,設(shè)置Cookie及設(shè)置cookie過(guò)期方法詳解,需要的朋友可以參考下2022-04-04
golang的匿名函數(shù)和普通函數(shù)的區(qū)別解析
匿名函數(shù)是不具名的函數(shù),可以在不定義函數(shù)名的情況下直接使用,通常用于函數(shù)內(nèi)部的局部作用域中,這篇文章主要介紹了golang的匿名函數(shù)和普通函數(shù)的區(qū)別,需要的朋友可以參考下2023-03-03
使用golang引入外部包的三種方式:go get, go module, ve
這篇文章主要介紹了使用golang引入外部包的三種方式:go get, go module, vendor目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
詳解Golang互斥鎖內(nèi)部實(shí)現(xiàn)
本篇文章主要介紹了詳解Golang互斥鎖內(nèi)部實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Golang實(shí)現(xiàn)Json分級(jí)解析及數(shù)字解析實(shí)踐詳解
你是否遇到過(guò)在無(wú)法準(zhǔn)確確定json層級(jí)關(guān)系的情況下對(duì)json進(jìn)行解析的需求呢?本文就來(lái)和大家介紹一次解析不確定的json對(duì)象的經(jīng)歷,以及遇到的問(wèn)題和解決方法2023-02-02
Golang map實(shí)踐及實(shí)現(xiàn)原理解析
這篇文章主要介紹了Golang map實(shí)踐以及實(shí)現(xiàn)原理,Go 語(yǔ)言中,通過(guò)哈希查找表實(shí)現(xiàn) map,用鏈表法解決哈希沖突,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06

