我尝试将图像上传到带节点的wordpress网站, 我得到了成功的回复,但当我查看图像时,我看不到图像。
节点代码:
let wordpress = require("./wordpress");
let base64 = require('node-base64-image');
let imageType = require('image-type');
module.exports = function(url) {
let promise = new Promise(function(resolve, reject) {
base64.encode(url, {}, function(err, data) {
if (err) {
return reject(err);
}
let base64Image = data.toString('base64');
let imageMime = imageType(data);
mediaData = {
type: imageMime.mime,
name: 'tzook.jpg',
overwrite: true,
bits: base64Image
}
wordpress.uploadFile(mediaData, function(error, id) {
if (error) {
return reject(error);
}
return resolve(id)
})
});
});
return promise;
}
在底线我传递了base64编码的字符串,我知道它在我检查字符串本身时起作用。
成功的回应:
{ attachment_id: '561',
date_created_gmt: 2016-07-04T03:29:01.000Z,
parent: 0,
link: 'http://www.mmayou.com/wp-content/uploads/2016/07/tzook-4.jpg',
title: 'tzook.jpg',
caption: '',
description: '',
metadata: '',
type: 'image/jpeg',
thumbnail: 'http://www.mmayou.com/wp-content/uploads/2016/07/tzook-4.jpg',
id: '561',
file: 'tzook.jpg',
url: 'http://www.mmayou.com/wp-content/uploads/2016/07/tzook-4.jpg' }
回复是
答案 0 :(得分:0)
它只是传递缓冲区本身而不是base64字符串......
代码:
//not this....
//let base64Image = data.toString('base64');
let imageMime = imageType(data);
mediaData = {
type: imageMime.mime,
name: 'tzook.jpg',
overwrite: true,
bits: data // but simply the buffer itself
}
答案来自:github repo https://github.com/scottgonzalez/node-wordpress/issues/44