欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Go語言實(shí)現(xiàn)文件上傳

 更新時(shí)間:2022年07月26日 09:03:01   作者:書香水墨  
這篇文章主要為大家詳細(xì)介紹了Go語言實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Golang 實(shí)現(xiàn)簡單隨機(jī)負(fù)載均衡

    Golang 實(shí)現(xiàn)簡單隨機(jī)負(fù)載均衡

    均衡算法又分為 隨機(jī),輪詢,加權(quán)輪詢,哈希,而隨機(jī)負(fù)載均衡算法就是本文的重點(diǎn),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • Go語言 go程釋放操作(退出/銷毀)

    Go語言 go程釋放操作(退出/銷毀)

    這篇文章主要介紹了Go語言 go程釋放操作(退出/銷毀),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Go?io/fs.FileMode文件系統(tǒng)基本操作和權(quán)限管理深入理解

    Go?io/fs.FileMode文件系統(tǒng)基本操作和權(quán)限管理深入理解

    這篇文章主要為大家介紹了Go?io/fs.FileMode文件系統(tǒng)基本操作和權(quán)限管理深入理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • 詳解Golang中g(shù)cache模塊的基本使用

    詳解Golang中g(shù)cache模塊的基本使用

    這篇文章主要通過結(jié)合商業(yè)項(xiàng)目的使用場景,為大家介紹了gcache的基本使用、緩存控制以及淘汰策略。使用gcache做緩存處理,簡單方便易上手
    2022-11-11
  • golang時(shí)間/時(shí)間戳的獲取與轉(zhuǎn)換實(shí)例代碼

    golang時(shí)間/時(shí)間戳的獲取與轉(zhuǎn)換實(shí)例代碼

    說實(shí)話,golang的時(shí)間轉(zhuǎn)化還是很麻煩的,最起碼比php麻煩很多,下面這篇文章主要給大家介紹了關(guān)于golang時(shí)間/時(shí)間戳的獲取與轉(zhuǎn)換的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Golang?Compare?And?Swap算法詳細(xì)介紹

    Golang?Compare?And?Swap算法詳細(xì)介紹

    CAS算法是一種有名的無鎖算法。無鎖編程,即不使用鎖的情況下實(shí)現(xiàn)多線程之間的變量同步,也就是在沒有線程被阻塞的情況下實(shí)現(xiàn)變量的同步,所以也叫非阻塞同步Non-blocking?Synchronization
    2022-10-10
  • Golang?Gin框架獲取請求參數(shù)的幾種常見方式

    Golang?Gin框架獲取請求參數(shù)的幾種常見方式

    在我們平常添加路由處理函數(shù)之后,就可以在路由處理函數(shù)中編寫業(yè)務(wù)處理代碼了,但在此之前我們往往需要獲取請求參數(shù),本文就詳細(xì)的講解下gin獲取請求參數(shù)常見的幾種方式,需要的朋友可以參考下
    2024-02-02
  • 淺析golang如何處理json中的null

    淺析golang如何處理json中的null

    json?是一種常用的數(shù)據(jù)格式,在?go?使用?json?序列化和反序列化時(shí)比較方便的,但在使用過程中,會遇到一些問題,比如?null,所以下面我們就來看看golang如何處理json中的null吧
    2023-09-09
  • Go語言開源庫實(shí)現(xiàn)Onvif協(xié)議客戶端設(shè)備搜索

    Go語言開源庫實(shí)現(xiàn)Onvif協(xié)議客戶端設(shè)備搜索

    這篇文章主要為大家介紹了Go語言O(shè)nvif協(xié)議客戶端設(shè)備搜索示例實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • Go語言fmt庫詳解與應(yīng)用實(shí)例(格式化輸入輸出功能)

    Go語言fmt庫詳解與應(yīng)用實(shí)例(格式化輸入輸出功能)

    fmt庫是Go語言中一個(gè)強(qiáng)大而靈活的庫,提供了豐富的格式化輸入輸出功能,通過本文的介紹和實(shí)例演示,相信你對fmt庫的使用有了更深的理解,感興趣的朋友一起看看吧
    2023-10-10

最新評論