上传不了视频

来源:4-5 第四天 荡平永久素材接口-上传

慕粉3278383

2016-05-13

为什么 上传视频是会 弄出3个 xml: 而且上传不了    而上传图片却可以  

{ xml:

   { ToUserName: [ 'gh_a1fa4a51afca' ],

     FromUserName: [ 'omt_gw-lQqAVp4-o1p_lQmxD0vC0' ],

     CreateTime: [ '1463145860' ],

     MsgType: [ 'text' ],

     Content: [ '6' ],

     MsgId: [ '6284163618388101594' ] } }

{ ToUserName: 'gh_a1fa4a51afca',

  FromUserName: 'omt_gw-lQqAVp4-o1p_lQmxD0vC0',

  CreateTime: '1463145860',

  MsgType: 'text',

  Content: '6',

  MsgId: '6284163618388101594' }

https://api.weixin.qq.com/cgi-bin/media/upload?&access_token=uxXEjVNDKQLe-snkIIq

yX-OL-cJFzGnAj-Y8vZbzxxKuUfpp9viuasIiY2Fb-Fax5j7tAtrIFdBRLd6vtoqR889m915tPoiVpjP

KXxTO3fquJ21OhUi6jlHqlLr4NY8QGSDeAJAKWH&type=video

{ xml:

   { ToUserName: [ 'gh_a1fa4a51afca' ],

     FromUserName: [ 'omt_gw-lQqAVp4-o1p_lQmxD0vC0' ],

     CreateTime: [ '1463145860' ],

     MsgType: [ 'text' ],

     Content: [ '6' ],

     MsgId: [ '6284163618388101594' ] } }

{ ToUserName: 'gh_a1fa4a51afca',

  FromUserName: 'omt_gw-lQqAVp4-o1p_lQmxD0vC0',

  CreateTime: '1463145860',

  MsgType: 'text',

  Content: '6',

  MsgId: '6284163618388101594' }

https://api.weixin.qq.com/cgi-bin/media/upload?&access_token=uxXEjVNDKQLe-snkIIq

yX-OL-cJFzGnAj-Y8vZbzxxKuUfpp9viuasIiY2Fb-Fax5j7tAtrIFdBRLd6vtoqR889m915tPoiVpjP

KXxTO3fquJ21OhUi6jlHqlLr4NY8QGSDeAJAKWH&type=video

{ xml:

   { ToUserName: [ 'gh_a1fa4a51afca' ],

     FromUserName: [ 'omt_gw-lQqAVp4-o1p_lQmxD0vC0' ],

     CreateTime: [ '1463145860' ],

     MsgType: [ 'text' ],

     Content: [ '6' ],

     MsgId: [ '6284163618388101594' ] } }

{ ToUserName: 'gh_a1fa4a51afca',

  FromUserName: 'omt_gw-lQqAVp4-o1p_lQmxD0vC0',

  CreateTime: '1463145860',

  MsgType: 'text',

  Content: '6',

  MsgId: '6284163618388101594' }

https://api.weixin.qq.com/cgi-bin/media/upload?&access_token=uxXEjVNDKQLe-snkIIq

yX-OL-cJFzGnAj-Y8vZbzxxKuUfpp9viuasIiY2Fb-Fax5j7tAtrIFdBRLd6vtoqR889m915tPoiVpjP

KXxTO3fquJ21OhUi6jlHqlLr4NY8QGSDeAJAKWH&type=video


写回答

5回答

慕莱坞8178471

2017-05-23

多次试验过后 在没有修改代码的情况下 我发现是网络的问题或者是ngrok问题 前面都是莫名其妙的三次的请求 最后一次显示出来了视频 说明代码没问题

1
0

慕粉3278383

提问者

2016-05-14

request(options).then(function(response) {
                    var _data = response.body
                    
      		    console.log(_data)
			
                    if (_data) {
                        resolve(_data)
                    }
                    else  {
                        throw new Error("upload material fails")
                    }
                })
                .catch(function (err) {
                    reject(err)
						
                console.log(err)	
				
                })

返回的 xml

{ xml:

   { ToUserName: [ 'gh_a1fa4a51afca' ],

     FromUserName: [ 'omt_gw-lQqAVp4-o1p_lQmxD0vC0' ],

     CreateTime: [ '1463231039' ],

     MsgType: [ 'text' ],

     Content: [ '6' ],

     MsgId: [ '6284529459407415752' ] } }

{ ToUserName: 'gh_a1fa4a51afca',

  FromUserName: 'omt_gw-lQqAVp4-o1p_lQmxD0vC0',

  CreateTime: '1463231039',

  MsgType: 'text',

  Content: '6',

  MsgId: '6284529459407415752' }

https://api.weixin.qq.com/cgi-bin/media/upload?&access_token=2whk8Z70SkOHfr1XMmv

4miLJvu9mY9M-dpXs8henWXbxCmFS5caIDIfCsN9Au5qZqbhPHjBE86-r8utkdzDGJ0DYhbVMFZ4a7bo

RiGazP4WrYT0okwKnOF2Un5cwuCrEKEGhAHAPJL&type=video

{ method: 'POST',

  url: 'https://api.weixin.qq.com/cgi-bin/media/upload?&access_token=2whk8Z70SkO

Hfr1XMmv4miLJvu9mY9M-dpXs8henWXbxCmFS5caIDIfCsN9Au5qZqbhPHjBE86-r8utkdzDGJ0DYhbV

MFZ4a7boRiGazP4WrYT0okwKnOF2Un5cwuCrEKEGhAHAPJL&type=video',

  json: true,

  formData:

   { media:

      ReadStream {

        _readableState: [Object],

        readable: true,

        domain: null,

        _events: [Object],

        _eventsCount: 1,

        _maxListeners: undefined,

        path: 'C:\\Users\\liuhui\\Desktop\\wechat\\aixin.mp4',

        fd: null,

        flags: 'r',

        mode: 438,

        start: undefined,

        end: undefined,

        autoClose: true,

        pos: undefined } } }

{ type: 'video',

  media_id: 'lUlF46h_9kVUyzm-EbkTI14Yxzln8xuWVtCSGDm8sq2XNyyy7MtNB-wTNXVkaj9i',

  created_at: 1463231051 }


0
0

Scott

2016-05-14

好奇怪,看样子像是 微信服务器 连续把一个消息推送过来 3 次的样子,要不是微信服务器抽风了,要不是这里上传逻辑有了问题,微信服务器没收到响应,又连续追加了 2 次消息推送。


request(options).then(function(response) {
                    var _data = response.body
                    if (_data) {
                        resolve(_data)
                    }
                    else  {
                        throw new Error("upload material fails")
                    }
                })
                .catch(function (err) {
                    reject(err)
                })


这块,把 _data 和 err 都打印一下,看这个请求中,是什么原因报错

0
1
albion
我也遇到了这个情况。reject 没有任何错误信息。_data也是三次不同的json
2016-05-14
共1条回复

慕粉3278383

提问者

2016-05-14


Wechat.prototype.uploadMaterial = function(type, material, permanent) {
var that = this
var form = {}
var uploadUrl = api.temporary.upload
if(permanent) {
uploadUrl = api.permanent.upload
_.extend(form,permanent)    //form适合所有图文信息  继承permanent 对象
}
if (type === 'pic') {
uploadUrl = api.permanent.uploadNewsPic
}
if (type === 'news') {
uploadUrl = api.permanent.uploadNews
form = material    //图文是 数据
}
else {
form.media = fs.createReadStream(material)   //图片或视频是 字符串路径
}
//发送get请求,本身是没有then的,我们用Promise进行了封装
return new Promise(function(resolve, reject){
        that
            .fetchAccessToken()
            .then(function(data) {
                var url = uploadUrl + "&access_token=" + data.access_token
if(!permanent) {
url += '&type=' + type
}
else {
form.access_token = data.access_token
}
console.log(url)
var options = {
method: 'POST',
url: url,
json: true
}
if (type === 'news') {    //图文
options.body = form
}
else {
options.formData = form
}
//console.log(options)
                request(options).then(function(response) {
                    var _data = response.body
                    if (_data) {
                        resolve(_data)
                    }
                    else  {
                        throw new Error("upload material fails")
                    }
                })
                .catch(function (err) {
                    reject(err)
                })
            })
})
}


else if (content === '6') {
			var data = yield wechatApi.uploadMaterial('video',  path.join(__dirname, '/aixin.mp4'))
			
			reply = {
				type: 'video',
				title: '回复视频内容',
				description: '粉红爱心',
				mediaId: data.media_id
			}

		}


0
0

Scott

2016-05-13

可以把相关代码贴出来,看一下是哪个环节出了问题

0
0

7天搞定Node.js微信公众号

Koa框架、ES2015新特性、MongoDB,开发微信公众号

1742 学习 · 787 问题

查看课程