Go语言文件上传

一.文件上传

  • 文件上传:客户端把上传文件转换为二进制流后发送给服务器,服务器对二进制流进行解析
  • HTML表单(form)enctype(Encode Type)属性控制表单在提交数据到服务器时数据的编码类型.
  • enctype=”application/x-www-form-urlencoded” 默认值,表单数据会被编码为名称/值形式
    • enctype=”multipart/form-data” 编码成消息,每个控件对应消息的一部分.请求方式必须是post
    • enctype=”text/plain” 纯文本形式进行编码的
  • HTML模版内容如下(在项目/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>
  • 服务端可以使用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					//文件内容,类型[]byte
	tmpfile string					//临时文件
}
  • 服务器端编写代码如下
    • 获取客户端传递后的文件流,把文件保存到服务器即可
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) {
   //获取普通表单数据
   username := r.FormValue("username")
   fmt.Println(username)
   //获取文件流,第三个返回值是错误对象
   file, header, _ := r.FormFile("photo")
   //读取文件流为[]byte
   b, _ := ioutil.ReadAll(file)
   //把文件保存到指定位置
   ioutil.WriteFile("D:/new.png", b, 0777)
   //输出上传时文件名
   fmt.Println("上传文件名:", header.Filename)
}

func main() {
   server := http.Server{Addr: "localhost:8899"}

   http.HandleFunc("/", welcome)
   http.HandleFunc("/upload", upload)

   server.ListenAndServe()
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书香水墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值