我正在尝试编写一个可以将图片上传到 wordpress 博客的 chrome 扩展程序。到目前为止,一切都很好,但是 wordpress rpc api 期待二进制图片:
http://codex.wordpress.org/XML-RPC_WordPress_API/Media#wp.uploadFile
不幸的是,我拥有的数据是 base64 编码的
http://developer.chrome.com/extensions/tabs.html#method-captureVisibleTab
触发我的代码时,我从 Wordpress 得到的唯一反馈是:faultCode -32700, parse error. not well formed
就是这个:
chrome.tabs.captureVisibleTab(null, {"format": "png"}, function(img) {
var data = new Object();
data.name = 'Screenshot' + Math.floor(Math.random() * 100); //pageTitle
data.overwrite = false;
data.type = 'image/png'; //string:File MIME type.
//transfrom screencapture into binary data for wordpress xmlrpc
var regex = /^data:.+\/(.+);base64,(.*)$/;
var matches = img.match(regex);
data.bits = atob(matches[2]); //string: binary data. Shouldn't be base64-encoded.
$.xmlrpc({
url: wp.url,
methodName: 'wp.uploadFile',
params: [0, wp.user, wp.password, data],
success: function(response, status, jqXHR) {
//this return the post id
alert("worked :-)" + status + "/" + response + "/" +
jqXHR.responseXML + "/" + response.url);
},
error: function(jqXHR, status, error) {
alert("fail :-(" + status + " " + error + " " + jqXHR.responseXML);
}
});
});
你知道我怎么能让这个工作吗?太感谢了!