为什么返回errcode: 41005,
来源:4-4 第四天 上传临时素材之图片和视频
慕粉3278383
2016-05-10
为什么返回 { errcode: 41005,
errmsg: 'media data missing hint: [3jnfcA0377e297]' }
明明路径是对的呀
else if (content === '5') {
var data = yield wechatApi.uploadMaterial('image', __dirname + '/mei.jpg')
console.log(data);
reply = {
type: 'image',
mediaId: data.media_id
}
console.log(reply)
}
写回答
2回答
-
慕粉3278383
提问者
2016-05-10
啊...还是不行.... 这是什么原因呀....
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) //继承perman 对象 } if (type === 'pic') { uploadUrl = api.permanent.uploadNewsPic } if (type === 'news') { uploadUrl = api.permanent.uploadNews form = material } else { form.media = fs.createReadStream(material) } 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 } var options = { method: 'POST', url: url, json: true } if(type === 'news') { options.body = form } else { options.formDate = form } request({method: 'POST', url: url, formDate: form, json: true}).then(function(response) { var _data = response.body if(_data) { resolve(_data); console.log(_data) } else { throw new Error('Upload material fails') } }) .catch(function(err) { reject(err) }) }) }) }
Wechat.prototype.fetchAccessToken = function(data) { var that = this if (this.access_token && this.expires_in) { if (this.isValidAccessToken(this)) { return Promise.resolve(this) } } return this.getAccessToken() //实现 promise .then(function(data) { try{ data = JSON.parse(data) //读出字符串 } catch(e) { //判断是否合法 return that.updateAccessToken() } if (that.isValidAccessToken(data)){ return Promise.resolve(data) //合法 通过此 把data 传下来 } else { return that.updateAccessToken() } }) .then(function(data) { that.access_token = data.access_token that.expires_in = data.expires_in // 将票据写入文件中 that.saveAccessToken(data) return Promise.resolve(data) }) }
else if (content === '5') { var data = yield wechatApi.uploadMaterial('image',path.join(__dirname, '/mei.jpg')) console.log(data); reply = { type: 'image', mediaId: data.media_id } console.log(reply) }
112016-10-18 -
Scott
2016-05-10
会不会是 accessToken 导致的呢?
http://coding.imooc.com/learn/questiondetail/209.html
然后对 __dirname + '/mei.jpg',改成 path.join(__dirname, '/mei.jpg') 抹平下不同操作系统下的路径问题
012016-10-19
相似问题