在我的node.js服务器中,我从另一台服务器下载文件。下载的文件是用Base64编码两次的JPG图像数据,这意味着我必须解码它2次。鉴于是我的代码。
var base64DecodedFileData = new Buffer(file_data, 'base64').toString('binary');
var tmp = base64DecodedFileData.split("base64,");
var base64DecodedFileData = new Buffer(tmp[1], 'base64').toString('binary');
var file = fs.createWriteStream(file_path, stream_options);
file.write(base64DecodedFileData);
file.end();
我知道我的图像数据在我第一次解码时是有效的(我已经通过第二次解码得到了正确的图像来验证在线base64解码器中的数据),但是当我第二次解码并创建时包含此数据的文件。我没有得到有效的JPG文件。
我将它与实际图像进行了比较,两个文件的开头和结尾看起来都不错,但在我构建的文件中有些不对。构造的文件的大小也比原始文件大。
PS:我在第二次解码之前进行拆分,因为第一次解码后的数据以
开始数据:; base64,DATASTARTS
任何想法。 Farrukh Arshad。
答案 0 :(得分:0)
我已经解决了我的问题。问题似乎是在node.js的解码中,所以我写了一个C ++插件来完成这项工作。这是代码。如果我们只对图像文件进行一次编码,我非常确定问题仍然存在。
.js文件
ModUtils.generateImageFromData(file_data,file_path);
c ++ addon:这使用了base64 C ++编码器/解码器 http://www.adp-gmbh.ch/cpp/common/base64.html
#define BUILDING_NODE_EXTENSION
#include <node.h>
#include <iostream>
#include <fstream>
#include "base64.h"
using namespace std;
using namespace v8;
static const std::string decoding_prefix =
"data:;base64,";
// --------------------------------------------------------
// Decode the image data and save it as image
// --------------------------------------------------------
Handle<Value> GenerateImageFromData(const Arguments& args) {
HandleScope scope;
// FIXME: Improve argument checking here.
// FIXME: Add error handling here.
if ( args.Length() < 2) return v8::Undefined();
Handle<Value> fileDataArg = args[0];
Handle<Value> filePathArg = args[1];
String::Utf8Value encodedData(fileDataArg);
String::Utf8Value filePath(filePathArg);
std::string std_FilePath = std::string(*filePath);
// We have received image data which is encoded with Base64 two times
// so we have to decode it twice.
std::string decoderParam = std::string(*encodedData);
std::string decodedString = base64_decode(decoderParam);
// After first decoding the data will also contains a encoding prefix like
// data:;base64,
// We have to remove this prefix to get actual encoded image data.
std::string second_pass = decodedString.substr(decoding_prefix.length(), (decodedString.length() - decoding_prefix.length()));
std::string imageData = base64_decode(second_pass);
// Write image to file
ofstream image;
image.open(std_FilePath.c_str());
image << imageData;
image.close();
return scope.Close(String::New(" "));
//return scope.Close(decoded);
}
void Init(Handle<Object> target) {
// Register all functions here
target->Set(String::NewSymbol("generateImageFromData"),
FunctionTemplate::New(GenerateImageFromData)->GetFunction());
}
NODE_MODULE(modutils, Init);
希望它对其他人有所帮助。