我正在尝试使用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)]);
console.log(b64string)
,请展示一些示例数据。 - loganfsmyth