为什么会有base64呢?
为了把二进制内容安全转成文本字符串。
比如:
1、邮件的附件,附件是base64编码嵌入在邮件中((小的附件)
2、后端把图片信息base64数据嵌入到API响应中,前端负责显示
cpp
bool MyFileUtil::ReadFileAsBase64(const std::wstring& filePath, std::string& base64FileData)
{
base64FileData.clear();
HANDLE hFile = ::CreateFileW(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return false;
}
std::unique_ptr<std::remove_pointer<HANDLE>::type, decltype(&::CloseHandle)> uh(hFile, ::CloseHandle);
DWORD fileSize = ::GetFileSize(hFile, NULL);
if (fileSize == 0)
{
return false;
}
DWORD readSize = 0;
std::unique_ptr<char[]> up(new char[fileSize]);
if (!::ReadFile(hFile, up.get(), fileSize, &readSize, nullptr) || readSize == 0)
{
return false;
}
MyUtil::Base64Encode(std::string(up.get(), readSize), base64FileData);
return !base64FileData.empty();
}