當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Client.PostMultipartForm方法代碼示例

本文整理匯總了Golang中github.com/chanxuehong/wechat/v2/mp/core.Client.PostMultipartForm方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.PostMultipartForm方法的具體用法?Golang Client.PostMultipartForm怎麽用?Golang Client.PostMultipartForm使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/chanxuehong/wechat/v2/mp/core.Client的用法示例。


在下文中一共展示了Client.PostMultipartForm方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: UploadImageFromReader

// 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
}
開發者ID:btbxbob,項目名稱:wechat,代碼行數:27,代碼來源:uploadimg.go

示例2: UploadThumbFromReader

// UploadThumbFromReader 上傳多媒體縮略圖
//  NOTE: 參數 filename 不是文件路徑, 是 multipart/form-data 裏麵 filename 的值.
func UploadThumbFromReader(clt *core.Client, filename string, reader io.Reader) (mediaId, url string, err error) {
	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=thumb&access_token="

	var fields = []core.MultipartFormField{
		{
			IsFile:   true,
			Name:     "media",
			FileName: filename,
			Value:    reader,
		},
	}
	var result struct {
		core.Error
		MediaId string `json:"media_id"`
		URL     string `json:"url"`
	}
	if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	mediaId = result.MediaId
	url = result.URL
	return
}
開發者ID:btbxbob,項目名稱:wechat,代碼行數:29,代碼來源:upload.go

示例3: uploadFromReader

func uploadFromReader(clt *core.Client, mediaType, filename string, reader io.Reader) (info *MediaInfo, err error) {
	var incompleteURL = "https://api.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType + "&access_token="

	var fields = []core.MultipartFormField{
		{
			IsFile:   true,
			Name:     "media",
			FileName: filename,
			Value:    reader,
		},
	}
	var result struct {
		core.Error
		MediaInfo
	}
	if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	info = &result.MediaInfo
	return
}
開發者ID:btbxbob,項目名稱:wechat,代碼行數:25,代碼來源:upload.go

示例4: addFromReader

func addFromReader(clt *core.Client, filename string, reader io.Reader, _type string) (info ImageInfo, err error) {
	var result struct {
		core.Error
		ImageInfo `json:"data"`
	}

	var incompleteURL string
	if _type != "" {
		incompleteURL = "https://api.weixin.qq.com/shakearound/material/add?type=" + url.QueryEscape(_type) +
			"&access_token="
	} else {
		incompleteURL = "https://api.weixin.qq.com/shakearound/material/add?access_token="
	}
	fields := []core.MultipartFormField{{
		IsFile:   true,
		Name:     "media",
		FileName: filename,
		Value:    reader,
	}}
	if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
		return
	}

	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	info = result.ImageInfo
	return
}
開發者ID:btbxbob,項目名稱:wechat,代碼行數:30,代碼來源:add.go

示例5: UploadVideoFromReader

// UploadVideoFromReader 上傳多媒體縮視頻.
//  NOTE: 參數 filename 不是文件路徑, 是 multipart/form-data 裏麵 filename 的值.
func UploadVideoFromReader(clt *core.Client, filename string, reader io.Reader, title, introduction string) (mediaId string, err error) {
	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=video&access_token="

	var description = struct {
		Title        string `json:"title"`
		Introduction string `json:"introduction"`
	}{
		Title:        title,
		Introduction: introduction,
	}
	descriptionBytes, err := json.Marshal(&description)
	if err != nil {
		return
	}

	var fields = []core.MultipartFormField{
		{
			IsFile:   true,
			Name:     "media",
			FileName: filename,
			Value:    reader,
		},
		{
			IsFile: false,
			Name:   "description",
			Value:  bytes.NewReader(descriptionBytes),
		},
	}
	var result struct {
		core.Error
		MediaId string `json:"media_id"`
	}
	if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	mediaId = result.MediaId
	return
}
開發者ID:btbxbob,項目名稱:wechat,代碼行數:44,代碼來源:upload.go

示例6: UploadHeadImageFromReader

// UploadHeadImageFromReader 上傳客服頭像.
//  NOTE: 參數 filename 不是文件路徑, 是 multipart/form-data 裏麵 filename 的值.
func UploadHeadImageFromReader(clt *core.Client, kfAccount, filename string, reader io.Reader) (err error) {
	// TODO
	//	incompleteURL := "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account=" +
	//		url.QueryEscape(kfAccount) + "&access_token="
	incompleteURL := "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account=" +
		kfAccount + "&access_token="

	var fields = []core.MultipartFormField{{
		IsFile:   true,
		Name:     "media",
		FileName: filename,
		Value:    reader,
	}}
	var result core.Error
	if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result
		return
	}
	return
}
開發者ID:btbxbob,項目名稱:wechat,代碼行數:25,代碼來源:head_img.go


注:本文中的github.com/chanxuehong/wechat/v2/mp/core.Client.PostMultipartForm方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。