上传永久素材

关于上传永久素材的接口说明,请参考:
https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html

首先新建一个表,表名为Materials,用于储存永久素材信息,表结构如下:

上传图片素材

上传图片素材的代码,可以在命令窗口测试:


Dim
url As String = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type=image"
Dim
hc As new HttpClient(CExp(url, Functions.Execute("GetAccessToken")))
hc
.Files.Add("media","c:\data\sample.jpg") '指定要上传的图片文件
Dim
jo As JObject = JObject.Parse( hc.getdata)
If
jo("errcode") Is Nothing Then
    Dim dr As DataRow = DataTables("Materials").AddNew()
   
dr("Type") = "image"
    dr("mediaID") = jo("media_id")
    dr("URL") = jo("url")
    dr("UpdateTime") = Date.Now
    dr("FileName") = "
sample.jpg"
    dr.Save()

Else

    MessageBox.Show(jo.ToString)

End
If

上传缩略图素材

上传缩略图素材的代码完全相同,只需将type参数改为thumb即可:


Dim
url As String = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type=thumb"
Dim
hc As new HttpClient(CExp(url, Functions.Execute("GetAccessToken")))
hc
.Files.Add("media","c:\data\006s.jpg") '指定要上传的图片文件
Dim
jo As JObject = JObject.Parse( hc.getdata)
If
jo("errcode") Is Nothing Then
    Dim dr As DataRow = DataTables("Materials").AddNew()
    dr("Type") = "thumb"
    dr("mediaID") = jo("media_id")
   
dr("URL") = jo("url")
    dr("UpdateTime") = Date.Now
   
dr("FileName") = "
006s.jpg"
    dr.Save()
Else

    MessageBox.Show(jo.ToString)

End
If

上传语音素材

上传语音素材的代码也基本相同,只需将type参数改为voice,另语音素材上传成功后不会返回URL:

Dim
url As String = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type=voice"
Dim
hc As new HttpClient(CExp(url, Functions.Execute("GetAccessToken")))
hc
.Files.Add("media","c:\data\sample.mp3") '指定要上传的语音文件
Dim
jo As JObject = JObject.Parse( hc.getdata)
If
jo("errcode") Is Nothing Then
    Dim dr As DataRow = DataTables("Materials").AddNew()
    dr("Type") = "voice"
    dr("mediaID") = jo("media_id")
    dr("UpdateTime") = Date.Now
   
dr("FileName") = "
sample.mp3"
    dr.Save()
Else

    MessageBox.Show(jo.ToString)

End
If

上传视频素材

根据接口说明,上传视频素材有点特殊,需要同时提交一个JSON格式的视频素材说明,代码可以参考:

Dim url As String = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type=video"
Dim
hc As New HttpClient(CExp(url, Functions.Execute("GetAccessToken")))
hc
.Files.Add("media","c:\data\samlpe.mp4") '指定要上传的视频文件
Dim
jo As New JObject
jo
("title") = "视频标题"
jo
("introduction") = "视频简介"
hc
.FormData.Add("description", jo.ToString)
jo
= JObject.Parse(hc.getdata)
If
jo("errcode") Is Nothing Then
    Dim dr As DataRow = DataTables("Materials").AddNew()
    dr("Type") = "video"
    dr("mediaID") = jo("media_id")
    dr("UpdateTime") = Date.Now
   
dr("FileName") = "
sample.mp4"
    dr.Save()

Else

    MessageBox.Show(jo.ToString)

End
If

上传图文素材

上传永久图文素材的代码参考:

Dim ar As New JArray
For
i As Integer = 1 To 1 '可以有多段图文
    Dim ao As New JObject
    ao("title") = "标题"
    ao("thumb_media_id") = "eHDdfBQmRtOJ-PtkD6WENiWR_BUO5GO3cWv7uZAGbIk" '缩略图ID
    ao("author") = "作者"
    ao("digest") = "图文消息摘要"
    ao("show_cover_pic") = "1" '显示封面
    ao("content") = "图文消息内容,可以包括HTML标签"
    ao("content_source_url") = "http://www.foxtable.com" '点击"阅读原文"后的URL
    ao("need_open_comment") = 1 '是否打开评论,0不打开,1打开
    ao("only_fans_can_comment") = 1 '是否粉丝才可评论,0所有人可评论,1粉丝才可评论
    ar.add(ao)

Next
Dim
jo As New JObject
jo
("articles") = ar
Dim
url As String = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={0}"
Dim
hc As new HttpClient(CExp(url, Functions.Execute("GetAccessToken")))
hc
.Content = jo.ToString()
jo
= JObject.Parse(hc.GetData)
If
jo("errcode") Is Nothing Then
    Dim dr As DataRow = DataTables("Materials").AddNew()
    dr("Type") = "news"
    dr("mediaID") = jo("media_id")
    dr("UpdateTime") = Date.Now
   
dr("Content") = ar.ToString
    dr.Save()

Else

    MessageBox.Show(jo.ToString)

End
If


本页地址:http://www.foxtable.com/mobilehelp/topics/0181.htm