Go语言实现文件上传
作者:书香水墨 时间:2023-07-08 18:26:38
本文实例为大家分享了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()
}
来源:https://blog.csdn.net/qq_27870421/article/details/118494346
标签:Go,文件上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用Python操作MySql数据库和MsSql数据库
2024-01-21 02:20:00
![](https://img.aspxhome.com/file/2023/7/127627_0s.png)
利用canvas实现的加载动画效果实例代码
2023-06-29 22:49:51
![](https://img.aspxhome.com/file/2023/1/55891_0s.gif)
如何运用python读写CSV文件
2021-11-13 04:35:36
![](https://img.aspxhome.com/file/2023/4/97674_0s.png)
Mysql使用索引实现查询优化
2024-01-16 03:59:35
ADO.Net对oracle数据库操作的实例代码
2024-01-18 02:00:41
Mysql查询语句优化技巧
2024-01-19 15:41:08
Gradio机器学习模型快速部署工具应用分享
2023-06-30 01:33:33
![](https://img.aspxhome.com/file/2023/6/62846_0s.jpg)
k8s在go语言中的使用及client 初始化简介
2024-02-03 18:10:51
浅析python中特殊文件和特殊函数
2023-01-22 23:26:26
python基于pygame实现飞机大作战小游戏
2021-04-27 07:59:17
![](https://img.aspxhome.com/file/2023/8/81528_0s.jpg)
python把ipynb文件转换成pdf文件过程详解
2022-07-27 23:50:24
![](https://img.aspxhome.com/file/2023/1/84971_0s.png)
Python学习笔记之Django创建第一个数据库模型的方法
2024-01-14 20:46:39
![](https://img.aspxhome.com/file/2023/0/84050_0s.jpg)
css分页放大镜效果
2008-11-02 15:35:00
关于pycharm中pip版本10.0无法使用的解决办法
2021-08-27 15:30:47
![](https://img.aspxhome.com/file/2023/9/85019_0s.jpg)
Python内置函数property()如何使用
2022-12-07 07:04:28
两个百度WEB面试题 怎么做?
2010-09-03 18:40:00
asp如何显示SQL数据库所有表的名称?
2010-06-08 09:30:00
python turtle库画一个方格和圆实例
2021-09-25 14:06:10
![](https://img.aspxhome.com/file/2023/9/93799_0s.png)
详解Python中dict与set的使用
2022-09-11 05:22:22
浅析Python中的序列化存储的方法
2022-09-28 19:21:26