let res = await cloud.downloadFile({ fileID: event.fileID })
let filebuffer = res.fileContent
let filename = event.filename
let sha256 = crypto.createHash('sha256').update(filebuffer).digest('hex')
var body = JSON.stringify({
'filename': filename, 'sha256': sha256
})
let method = 'POST'
let url = '/v3/merchant/media/upload'
// let jsonStr = JSON.stringify({ filename, sha256 })
var timestamp = Date.parse(new Date()) / 1000
const randomstr = '95E9D72DB0389359EB54850779354B21'
var data = method + '\n' +
url + '\n' +
timestamp + '\n' +
randomstr + '\n' +
body + '\n'
// console.log('data>>>>>>>>>>>>>>>>>',data)
let sign = crypto.createSign('RSA-SHA256');
sign.update(new Buffer(data, 'utf-8'));
let sig = sign.sign(privateKey, 'base64');
console.log(sig)
body = '--boundary'+'\n'+'\r\n'
+'Content-Disposition: form-data; name="meta";'+'\n'+'\r\n'
+'Content-Type: application/json' + '\n'+'\r\n'
+body+'\n'+'\r\n'
+'--boundary'+'\n'+'\r\n'
+'Content-Disposition: form-data; name="file"; filename="'+filename+'";'+'\n'+'\r\n'
+'Content-Type: image/png'+'\n'+'\r\n'
+'\n'+'\r\n'
+filebuffer+'\n'+'\r\n'
+'--boundary--'+'\n'+'\r\n'
console.log(body)
// //////////////////////////////////////////
var mchid = 'xxxxxx'
var serial_no = 'xxxxxxxxx'
var options = {
method: method,
headers: {
'Accept': 'application/json',
// 'Content-Type':'application/json',
'Content-Type': 'multipart/form-data;boundary=boundary',
'Accept-Charset': 'utf-8',
'User-Agent': 'my-reddit-client',
'Authorization': 'WECHATPAY2-SHA256-RSA2048 mchid="'+mchid+'",nonce_str="'+randomstr+'",signature="'+sig+'",timestamp="'+timestamp+'",serial_no="'+serial_no+'"'
},
body: body,
uri: "https://api.mch.weixin.qq.com"+url
};
return rp(options).then(
function (res) {
console.log(res)
return res
}
).catch(
function (err) {
console.log(err)
return err
}
)
'Content-Type': 'multipart/form-data;boundary=boundary',
应该是'Content-Type': 'multipart/form-data;boundary='+key,
可以看看这个例子https://juejin.im/post/6844903752604844045
补/替换一下你的代码:
let timestamp |=0; let sign = crypto.createSign('sha256WithRSAEncryption');
PS:你有没有尝试过 form-data & wechatpay-axios-plugin 包 上传文件?
下面是我的代码,不知哪里有问题?
let res = await cloud.downloadFile({
fileID: event.fileID
})
let filebuffer = res.fileContent
let filename = event.filename
const imageMeta = {
filename: filename,
// easy calculated by the command `sha256sum hellowechatpay.png` on OSX
// or by require('wechatpay-axios-plugin').Hash.sha256(filebuffer)
// sha256: '1a47b1eb40f501457eaeafb1b1417edaddfbe7a4a8f9decec2d330d1b4477fbe',
sha256: require('wechatpay-axios-plugin').Hash.sha256(filebuffer)
}
const imageData = new FormData()
imageData.append('meta', JSON.stringify(imageMeta), {
contentType: 'application/json'
})
imageData.append('file', createReadStream('../images/mxbc.png'))
client.post('/v3/marketing/favor/media/image-upload', imageData, {
meta: imageMeta,
headers: imageData.getHeaders()
}).then(res => {
console.info(res.data.media_url)
return res
}).catch(error => {
console.error(error)
})