一.文件上传
- 文件上传:客户端把上传文件转换为二进制流后发送给服务器,服务器对二进制流进行解析
- HTML表单(form)enctype(Encode Type)属性控制表单在提交数据到服务器时数据的编码类型.
- enctype=”application/x-www-form-urlencoded” 默认值,表单数据会被编码为名称/值形式
- enctype=”multipart/form-data” 编码成消息,每个控件对应消息的一部分.请求方式必须是post
- enctype=”text/plain” 纯文本形式进行编码的
- HTML模版内容如下(在项目/view/index.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”)获取上传到的文件,官方定义如下
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
}
type File interface {
io.Reader
io.ReaderAt
io.Seeker
io.Closer
}
type FileHeader struct {
Filename string
Header textproto.MIMEHeader
Size int64
content []byte
tmpfile string
}
package main
import (
"net/http"
"fmt"
"html/template"
"io/ioutil"
)
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")
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()
}
一.文件下载简介
- 文件下载总体步骤
- 客户端向服务端发起请求,请求参数包含要下载文件的名称
- 服务器接收到客户端请求后把文件设置到响应对象中,响应给客户端浏览器
- 载时需要设置的响应头信息
- Content-Type: 内容MIME类型
- application/octet-stream 任意类型
- Content-Disposition:客户端对内容的操作方式
- inline 默认值,表示浏览器能解析就解析,不能解析下载
- attachment;filename=下载时显示的文件名 ,客户端浏览器恒下载
二.代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="download?filename=abc.png">下载</a>
</body>
</html>
package main
import (
"net/http"
"html/template"
"io/ioutil"
)
func showDownloadPage(rw http.ResponseWriter,r *http.Request){
t,_:=template.ParseFiles("template/html/download.html")
t.Execute(rw,nil)
}
func download(rw http.ResponseWriter,r *http.Request){
fn:=r.FormValue("filename")
header:=rw.Header()
header.Add("Content-Type","application/octet-stream")
header.Add("Content-Disposition","attachment;filename="+fn)
b,_:=ioutil.ReadFile("D:/"+fn)
rw.Write(b)
}
func main() {
server:=http.Server{Addr:"localhost:8899"}
http.HandleFunc("/showdownload",showDownloadPage)
http.HandleFunc("/download",download)
server.ListenAndServe()
}