点一个加载附件的按钮,然后选取所需图片:
void CForwardReq::AddAdjunct() //添加附件
{
unsigned int filelen; //附件的大小
char pMMbody[1024*1024]; 附件的内容体
static char BASED_CODE szFilter[] = "Media Files (*.jpg)|*.jpg|Media Files (*.bmp)|*.bmp|Media Files (*.gif)|*.gif|Media Files (*.mid)|*.mid|Media Files (*.wav)|*.wav|Media Files (*.*)|*.*|";
CFileDialog dlg(TRUE, "*.*", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,szFilter);
if(dlg.DoModal() == IDOK)
{
POSITION pos;
pos = dlg.GetStartPosition();
CString filename; while(pos)
{
//pBuff = pReservBuff;
if(!(filename = dlg.GetNextPathName(pos)))
break;
CFile file;
if(!file.Open(filename,CFile::modeRead|CFile::typeBinary,NULL))
{
AfxMessageBox("文件操作出错!");
return;
}
filelen = file.GetLength();
if(filelen > MMBODY_MAX_LEN){
AfxMessageBox("附件过大");
return;
}
file.Read(pMMbody,filelen);//读入所添加的附件内容
int aaa = lstrlen(pMMbody);
file.Close();
}
}
}
------------------------------------------
该程序读文本的时候没问题,int aaa = lstrlen(pMMbody); aaa的大小与文本的大小一致。 但附件为图片的时候就不对了,aaa的值为4,图片实际大小为96k了,这是为什么??
void CForwardReq::AddAdjunct() //添加附件
{
unsigned int filelen; //附件的大小
char pMMbody[1024*1024]; 附件的内容体
static char BASED_CODE szFilter[] = "Media Files (*.jpg)|*.jpg|Media Files (*.bmp)|*.bmp|Media Files (*.gif)|*.gif|Media Files (*.mid)|*.mid|Media Files (*.wav)|*.wav|Media Files (*.*)|*.*|";
CFileDialog dlg(TRUE, "*.*", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,szFilter);
if(dlg.DoModal() == IDOK)
{
POSITION pos;
pos = dlg.GetStartPosition();
CString filename; while(pos)
{
//pBuff = pReservBuff;
if(!(filename = dlg.GetNextPathName(pos)))
break;
CFile file;
if(!file.Open(filename,CFile::modeRead|CFile::typeBinary,NULL))
{
AfxMessageBox("文件操作出错!");
return;
}
filelen = file.GetLength();
if(filelen > MMBODY_MAX_LEN){
AfxMessageBox("附件过大");
return;
}
file.Read(pMMbody,filelen);//读入所添加的附件内容
int aaa = lstrlen(pMMbody);
file.Close();
}
}
}
------------------------------------------
该程序读文本的时候没问题,int aaa = lstrlen(pMMbody); aaa的大小与文本的大小一致。 但附件为图片的时候就不对了,aaa的值为4,图片实际大小为96k了,这是为什么??
我用你上面的代码运行时
为什么会提示有错
就是"error C2018: unknown character '0xb8'"
好象是这个定义有错char pMMbody[1024*1024]
望指教 谢谢
是你拷来的有中有一个字符是不对的,那一行你看看哪里有字符是错的,不是英文的输入输入的也会这样的````
麻烦给我指点一下啊,我知道肯定有错误,就是不知道哪不对,为什么不对。int aaa = lstrlen(pMMbody);这个我只是有来测试的。 关键是:file.Read(pMMbody,filelen);//读入所添加的附件内容 , 图片的内容没有完全读到pMMbody中,不知道是哪里写的不对?不知道正确的写法是什么,是不是读取图片还需要格外的编解码?
这个计算的是pMMbody中到第一个字符为0x0的长度.也就是只能取文本文件的长度.直接
int aaa = filelen; 就可以了
你说是我拷来的有中有一个字符是不对的,那一行你看看哪里有字符是错的,不是英文的输入输入的也会这样的```提示就是char pMMbody[1024*1024] 有错
但我不知道思念原因
望指教
->char pMMbody[1024*1024]; //附件的内容体
CFileDialogdlg(TRUE, "*.*", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,szFilter);
->CFileDialog dlg(TRUE, "*.*", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,szFilter);MMBODY_MAX_LEN再定义下就没有问题了`
{
unsigned char c1, c2, c3; // 输入缓冲区读出3个字节
int nDstLen = 0; // 输出的字符计数
int nLineLen = 0; // 输出的行长度计数
int nDiv = nSrcLen / 3; // 输入数据长度除以3得到的倍数
int nMod = nSrcLen % 3; // 输入数据长度除以3得到的余数 // 每次取3个字节,编码成4个字符
for (int i = 0; i < nDiv; i ++)
{
// 取3个字节
c1 = *pSrc++;
c2 = *pSrc++;
c3 = *pSrc++; // 编码成4个字符
*pDst++ = EnBase64Tab[c1 >> 2];
*pDst++ = EnBase64Tab[((c1 << 4) | (c2 >> 4)) & 0x3f];
*pDst++ = EnBase64Tab[((c2 << 2) | (c3 >> 6)) & 0x3f];
*pDst++ = EnBase64Tab[c3 & 0x3f];
nLineLen += 4;
nDstLen += 4; // 输出换行?
if (nLineLen > nMaxLineLen - 4)
{
*pDst++ = ' ';
*pDst++ = ' ';
nLineLen = 0;
nDstLen += 2;
}
} // 编码余下的字节
if (nMod == 1)
{
c1 = *pSrc++;
*pDst++ = EnBase64Tab[(c1 & 0xfc) >> 2];
*pDst++ = EnBase64Tab[((c1 & 0x03) << 4)];
*pDst++ = '=';
*pDst++ = '=';
nLineLen += 4;
nDstLen += 4;
}
else if (nMod == 2)
{
c1 = *pSrc++;
c2 = *pSrc++;
*pDst++ = EnBase64Tab[(c1 & 0xfc) >> 2];
*pDst++ = EnBase64Tab[((c1 & 0x03) << 4) | ((c2 & 0xf0) >> 4)];
*pDst++ = EnBase64Tab[((c2 & 0x0f) << 2)];
*pDst++ = '=';
nDstLen += 4;
} // 输出加个结束符
*pDst = '
int EncodeBase64(const unsigned char* pSrc, char* pDst, int nSrcLen, int nMaxLineLen)
{
unsigned char c1, c2, c3; // 输入缓冲区读出3个字节
int nDstLen = 0; // 输出的字符计数
int nLineLen = 0; // 输出的行长度计数
int nDiv = nSrcLen / 3; // 输入数据长度除以3得到的倍数
int nMod = nSrcLen % 3; // 输入数据长度除以3得到的余数
// 每次取3个字节,编码成4个字符
for (int i = 0; i < nDiv; i ++)
{
// 取3个字节
c1 = *pSrc++;
c2 = *pSrc++;
c3 = *pSrc++;
// 编码成4个字符
*pDst++ = EnBase64Tab[c1 >> 2];
*pDst++ = EnBase64Tab[((c1 << 4) | (c2 >> 4)) & 0x3f];
*pDst++ = EnBase64Tab[((c2 << 2) | (c3 >> 6)) & 0x3f];
*pDst++ = EnBase64Tab[c3 & 0x3f];
nLineLen += 4;
nDstLen += 4;
// 输出换行?
if (nLineLen > nMaxLineLen - 4)
{
*pDst++ = '\r';
*pDst++ = '\n';
nLineLen = 0;
nDstLen += 2;
}
}
// 编码余下的字节
if (nMod == 1)
{
c1 = *pSrc++;
*pDst++ = EnBase64Tab[(c1 & 0xfc) >> 2];
*pDst++ = EnBase64Tab[((c1 & 0x03) << 4)];
*pDst++ = '=';
*pDst++ = '=';
nLineLen += 4;
nDstLen += 4;
}
else if (nMod == 2)
{
c1 = *pSrc++;
c2 = *pSrc++;
*pDst++ = EnBase64Tab[(c1 & 0xfc) >> 2];
*pDst++ = EnBase64Tab[((c1 & 0x03) << 4) | ((c2 & 0xf0) >> 4)];
*pDst++ = EnBase64Tab[((c2 & 0x0f) << 2)];
*pDst++ = '=';
nDstLen += 4;
}
// 输出加个结束符
*pDst = '\0';
return nDstLen;
}Base64解码方法中,最简单的也是查表法:将64个可打印字符的值作为索引,查表得到的值(范围为0-63)依次连起来,拼凑成字节形式输出,就得到解码结果。 const char DeBase64Tab[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62, // '+'
0, 0, 0,
63, // '/'
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
0, 0, 0, 0, 0, 0,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
};
int DecodeBase64(const char* pSrc, unsigned char* pDst, int nSrcLen)
{
int nDstLen; // 输出的字符计数
int nValue; // 解码用到的长整数
int i;
i = 0;
nDstLen = 0;
// 取4个字符,解码到一个长整数,再经过移位得到3个字节
while (i < nSrcLen)
{
if (*pSrc != '\r' && *pSrc!='\n')
{
nValue = DeBase64Tab[*pSrc++] << 18;
nValue += DeBase64Tab[*pSrc++] << 12;
*pDst++ = (nValue & 0x00ff0000) >> 16;
nDstLen++;
if (*pSrc != '=')
{
nValue += DeBase64Tab[*pSrc++] << 6;
*pDst++ = (nValue & 0x0000ff00) >> 8;
nDstLen++;
if (*pSrc != '=')
{
nValue += DeBase64Tab[*pSrc++];
*pDst++ =nValue & 0x000000ff;
nDstLen++;
}
}
i += 4;
}
else // 回车换行,跳过
{
pSrc++;
i++;
}
}
// 输出加个结束符
*pDst = '\0';
return nDstLen;
}
int CMM7Class::Base64Enc(unsigned char *buf,const unsigned char*text,int size)
{
static char *base64_encoding =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int buflen = 0; while(size>0){
*buf++ = base64_encoding[ (text[0] >> 2 ) & 0x3f];
if(size>2){
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0xF) << 2) | (text[2] >> 6)];
*buf++ = base64_encoding[text[2] & 0x3F];
}else{
switch(size){
case 1:
*buf++ = base64_encoding[(text[0] & 3) << 4 ];
*buf++ = '=';
*buf++ = '=';
break;
case 2:
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0x0F) << 2) | (text[2] >> 6)];
*buf++ = '=';
break;
}
} text +=3;
size -=3;
buflen +=4;
} *buf = 0;
return buflen;
} char CMM7Class::GetBase64value(char ch)
{
if ((ch >= 'A') && (ch <= 'Z'))
return ch - 'A';
if ((ch >= 'a') && (ch <= 'z'))
return ch - 'a' + 26;
if ((ch >= '0') && (ch <= '9'))
return ch - '0' + 52;
switch (ch) {
case '+':
return 62;
case '/':
return 63;
case '=': /* base64 padding */
return 0;
default:
return 0;
}
}//进行base64解码输入应该是4的倍数(根据mime标准)
//如果不是4倍数返回错误
//注意 如果是最后一个字符 那么长度不准备 可能会多1
//返回buf长度
int CMM7Class::Base64Dec(unsigned char *buf,const unsigned char*text,int size)
{
if(size%4)
return -1;
unsigned char chunk[4];
int parsenum=0;while(size>0){
chunk[0] = GetBase64value(text[0]);
chunk[1] = GetBase64value(text[1]);
chunk[2] = GetBase64value(text[2]);
chunk[3] = GetBase64value(text[3]); *buf++ = (chunk[0] << 2) | (chunk[1] >> 4);
*buf++ = (chunk[1] << 4) | (chunk[2] >> 2);
*buf++ = (chunk[2] << 6) | (chunk[3]); text+=4;
size-=4;
parsenum+=3;
}return parsenum;
}
其次获取读取读取的数据大小可以:
int aaa = file.Read(pMMbody,filelen);//读入所添加的附件内容
aaa中就是实际读取的字节数。
错误是OxC000000FD, Stack Overflow。
CFile file(mediaObject.strFilePath, CFile::modeRead);
DWORD dwLength = file.GetLength();
void far *p = ::GlobalAlloc(0, dwLength);
if (p == NULL)
{
::AfxMessageBox(_T("Alloc memory error"));
return FALSE;
}
file.ReadHuge(p, dwLength);
file.Close();
char* pBase64 = new char[dwLength * 2 + 4];
::Base64Encode(pBase64, (LPCTSTR)p, dwLength);//用base64编码
mediaObject.strBase64Code.Format(_T("%s"), pBase64);
::GlobalFree(p);
delete pBase64;
附base64.h,这个是从网上下的,应该没有问题,我还用了另一个算法,也是同样的错误。
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// base64.h
// 参考文章:http://www.cstc.net.cn/docs/docs.php?id=202
//************************************************************************/
// base64编码表
//
// 0 A 17 R 34 i 51 z
// 1 B 18 S 35 j 52 0
// 2 C 19 T 36 k 53 1
// 3 D 20 U 37 l 54 2
// 4 E 21 V 38 m 55 3
// 5 F 22 W 39 n 56 4
// 6 G 23 X 40 o 57 5
// 7 H 24 Y 41 p 58 6
// 8 I 25 Z 42 q 59 7
// 9 J 26 a 43 r 60 8
// 10 K 27 b 44 s 61 9
// 11 L 28 c 45 t 62 +
// 12 M 29 d 46 u 63 /
// 13 N 30 e 47 v (pad) =
// 14 O 31 f 48 w
// 15 P 32 g 49 x
// 16 Q 33 h 50 y
//
// base64编码步骤:
--------------------------------------
OxC000000FD, Stack Overflow
-----------------------------
这个错误信息的意思是,栈溢出。
可能的原因一:递归层次太多
可能的原因二:局部变量太大
我没有仔细看楼主的算法,楼主可以从栈的使用情况去分析问题所在。