forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uploadimg.go
49 lines (43 loc) · 1.24 KB
/
uploadimg.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
package base
import (
"io"
"os"
"path/filepath"
"github.com/bububa/wechat/mp/core"
)
// UploadImage 上传图片到微信服务器, 返回的图片url给其他场景使用, 比如图文消息, 卡卷, POI.
func UploadImage(clt *core.Client, imgFilePath string) (url string, err error) {
file, err := os.Open(imgFilePath)
if err != nil {
return
}
defer file.Close()
return UploadImageFromReader(clt, filepath.Base(imgFilePath), file)
}
// UploadImageFromReader 上传图片到微信服务器, 返回的图片url给其他场景使用, 比如图文消息, 卡卷, POI.
//
// NOTE: 参数 filename 不是文件路径, 是 multipart/form-data 里面 filename 的值.
func UploadImageFromReader(clt *core.Client, filename string, reader io.Reader) (url string, err error) {
const incompleteURL = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token="
var fields = []core.MultipartFormField{
{
IsFile: true,
Name: "media",
FileName: filename,
Value: reader,
},
}
var result struct {
core.Error
URL string `json:"url"`
}
if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
url = result.URL
return
}