/
file.go
52 lines (48 loc) · 1.22 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package httpcli
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
)
var (
stdCli = NewClient()
)
// 通过HTTP上传文件
// 模拟<form ...><input name="file" type="file" />...</form>
// 参数说明
// url: 上传服务器URL
// form_name: 对应<input>标签中的name
// file_name: 为form表单中的文件名
// path: 为实际要上传的本地文件路径
func PostFile(url, form_name, file_name, path string) (res *http.Response, err error) {
buf := new(bytes.Buffer) // caveat IMO dont use this for large files, \
// create a tmpfile and assemble your multipart from there (not tested)
w := multipart.NewWriter(buf)
fw, err := w.CreateFormFile(form_name, file_name) //这里的file必须和服务器端的FormFile一致
if err != nil {
return
}
fd, err := os.Open(path)
if err != nil {
return
}
defer fd.Close()
// Write file field from file to upload
_, err = io.Copy(fw, fd)
if err != nil {
return
}
// Important if you do not close the multipart writer you will not have a
// terminating boundry
w.Close()
req, err := http.NewRequest("POST", url, buf)
if err != nil {
return
}
req.Header.Set("Content-Type", w.FormDataContentType())
var client http.Client
res, err = client.Do(req)
return
}