【发布时间】:2013-04-19 07:42:18
【问题描述】:
我正在尝试使用 Node.js 将 base64 图像上传到 Facebook 页面。如果我从文件系统读取文件(即使用 fs.readFileSync('c:\a.jpg')
但是,我是否应该使用 base64 编码的图像并尝试上传它,它会给我以下错误:{"error":{"message":"(#1) An unknown error occurred","type":"OAuthException","code":1}}
我尝试通过new Buffer(b64string, 'base64'); 将其转换为二进制并上传,但没有成功。
我已经为此苦苦挣扎了 3 天,因此将不胜感激。
编辑:如果有人也知道我如何将 base64 转换为二进制并成功上传,那也适用于我。
编辑:代码片段
var postDetails = separator + newlineConstant + 'Content-Disposition: form-data;name="access_token"' + newlineConstant + newlineConstant + accessToken + newlineConstant + separator;
postDetails = postDetails + newlineConstant + 'Content-Disposition: form-data; name="message"' + newlineConstant + newlineConstant + message + newlineConstant;
//Add the Image information
var fileDetailsString = '';
var index = 0;
var multipartBody = new Buffer(0);
images.forEach(function (currentImage) {
fileDetailsString = fileDetailsString + separator + newlineConstant + 'Content-Disposition: file; name="source"; filename="Image' + index + '"' + newlineConstant + 'Content-Type: image/jpeg' + newlineConstant + newlineConstant;
index++;
multipartBody = Buffer.concat([multipartBody, new Buffer(fileDetailsString), currentImage]); //This is what I would use if Bianry data was passed in
currentImage = new Buffer (currentImage.toString('base64'), 'base64'); // The following lines are what I would use for base64 image being passed in (The appropriate lines would be enabled/disabled if I was using Binary/base64)
multipartBody = Buffer.concat([multipartBody, new Buffer(fileDetailsString), currentImage]);
});
multipartBody = Buffer.concat([new Buffer(postDetails), multipartBody, new Buffer(footer)]);
【问题讨论】:
-
b64String 来自哪里?你确定它不是数据 URL 吗?如果您
console.log(b64string),请显示一些示例数据。 -
您能否提供通过javascript ajax上传base64的任何示例。
-
大家好,很抱歉回复晚了……我已经离开了。它绝对不是数据 URL...这里是数据开头的 sn-p.../9j/4AAQSkZJRgABAQEASABIAAD/2wBDABcQERQRDhcUEhQaGBcbIjk @Brune 我已更新帖子以放置上传部分的 sn-p。多部分数据等都可以正常工作,因为当我传递了一个二进制图像时,它都可以 100% 工作,但是当我传递了一个 base64 图像时,它就不起作用了。
-
这与 Node.js 有关系吗?我有 base64 图像数据,并且对 ajax 请求的结构感到震惊(标头和正文中应该包含什么)。我遇到了无效请求错误。 .
标签: javascript node.js facebook-graph-api encoding base64