测试了几十遍,一直传不上数据,一直返回{ errcode: 41005, errmsg: 'media data missing hint: [NM04491543]' },在此跪求大佬解决。
我的代码如下,写的不好望大佬指教,最好解释一下。
url: `https: //api.weixin.qq.com/wxa/img_sec_check?access_token=${access_token}`, method: 'POST' , json: true , form: { media: fs.readFileSync(file.path), // 其中file.path是图片路径,fs.readFileSync(file.path)返回的是一个Buffer }, }, (err, response, body) => { console.log(body); }) |
https://developers.weixin.qq.com/community/develop/doc/00000ef32dc64883d9d93f31f56800 这个链接解决了问题
重大消息!!重大消息!!
时隔两个月,当我重新开展这个项目的时候,终于花了4个小时,综合各路消息解决了。
废话不多说,直接上代码
const fs = require('fs'); const request = require('request'); request({ url: `https://api.weixin.qq.com/wxa/img_sec_check?access_token=${access_token}`, method: "POST", //查了很多资料,虽然搞不懂原理,但必须这样写!! formData: { buffer: { value: fs.readFileSync(temp_file.path), // 其中temp_file.path是图片路径,fs.readFileSync(temp_file.path)返回的是一个Buffer options: { filename: temp_file.name, // 文件名,随便填,但必须要有 contentType: temp_file.type, // 文件类型,比如'image/jpeg' } }, }, }, (error, response, body) => { console.log(body) })
请问解决没有
改用云调用吧。
网上找的,试试看
var request = require('request');
var FormData = require('form-data');
var form = new FormData();
form.append("media", fs.readFileSync(file.path));
form.getLength(function(err, length){
if (err) {
return requestCallback(err);
}
var r = request.post("https://api.weixin.qq.com/wxa/img_sec_check?access_token=${access_token}", requestCallback);
r._form = form;
r.setHeader('content-length', length);
});
function requestCallback(err, res, body) {
console.log(body);
}
const cloud = require('wx-server-sdk')
然后调用
cloud.openapi.security.imgSecCheck({
media: {
contentType: 'image/png',
value: Buffer
}
})
var max = 9007199254740992;
var dec = Math.random() * max;
var hex = dec.toString(36);
var boundary = '---------------------------' + BOUNDARYPREFIX + hex;
var data = '--' + boundary + "\r\n";
// form data
var formdata = "content-disposition: form-data; name=\"access_token\"\r\n";
formdata += "content-type: text/plain\r\n\r\n";
formdata += param.access_token + "\r\n";
formdata += '--' + boundary + "\r\n";
var mimes = {
'.png': 'image/png',
'.gif': 'image/gif',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg'
};
var ext = path.extname(param.media);
var mime = mimes[ext];
// file data
var filedata = '';
filedata += "content-disposition: form-data; name=\"media\"; filename=\"" + param.media.split('/').pop() + "\"\r\n";
filedata += "content-type: " + mime + "\r\n\r\n";
filedata += fs.readFileSync(param.media) + "\r\n";
filedata += '--' + boundary + "\r\n";
data += formdata + filedata + "--\r\n\r\n";
return { data, boundary };
}
return new Promise((rs, rj) => {
var pdata = formData({ media: file.path, access_token: access_token })
var body = '';
const req = https.request({
hostname: 'api.weixin.qq.com',
port: 443,
method: 'POST',
path: '/wxa/img_sec_check?access_token=' + access_token,
headers: {
"content-type": "multipart/form-data; boundary=" + pdata.boundary,
'Content-Length': Buffer.byteLength(pdata.data)
}
}, (res) => {
res.on('data', function (d) {
body += d;
})
res.on('end', function () {
rs(JSON.parse(body))
})
});
req.on('error', (e) => {
rj(e);
});
req.write(pdata.data);
req.end();
})