日期:2014-05-19  浏览次数:20903 次

邮件中的附件base64编码生成文件问题,急!~
问题描述:
eml格式中的邮件附件部分是使用base64的编码,我把此编码成功解码后,用什么方法来还原(生成)此附件。我发现如果是文本类型的附件使用StreamWriter就可以生成,但对于其他类型的文件(.jpg/.exe/.rar等),使用StreamWriter或者FileStream中的方法都不能成功生成相关文件。
我的问题是:怎样才能由编码生成相应的文件(文件类型已知的情况下)。

例如:以下编码是一个名为msdn.jpg的base64信息,怎么样把下列编码成功还原为msdn.jpg的图片(base64解码部分我已经搞定):
/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFk
b2JlAGTAAAAAAf/bAIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAM
DAwMDAwQDA4PEA8ODBMTFBQTExwbGxscHx8fHx8fHx8fHwEHBwcNDA0YEBAY
GhURFRofHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8f
Hx8fHx8fHx8f/8AAEQgAKgBoAwERAAIRAQMRAf/EAJMAAQACAwEBAQAAAAAA
AAAAAAAEBwEFBgMCCAEBAAIDAQAAAAAAAAAAAAAAAAEEAwUGAhAAAgEDAwMB
BQYDCQAAAAAAAQIDAAQFERIGITEHE1EyMxQIQWGBkSJCcYIWobHhUmKDNIQV
EQEAAgEBBgMIAwAAAAAAAAAAAQIDBBEhMYESBUHBIvBRYXGRseFCUjM0/9oA
DAMBAAIRAxEAPwD9U0CgUCgUCgUFLZLmHJo/qbxXGUyMq4Ga1Z5ceCPSZhZT
yakaa++gNErpogoFAoFAoKF+qzOZ3FW3FnxF7cWkstxchlt5ZI/UKiLarBCu
7qe1Ew5ny5wDnXD+P2/OJOaX95nBcRLfqHeKNXlB+BtbQKjDbt26FfZ2oPfJ
8E57ynxvceSsvy27iy5sZMnb423LRWsdtCpkVFCOu1mjTd0Hc9de9Bps55b5
7L4GwksF9Ol1Nf3GNyOYRiJ2SBRJChlB3Bn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2Ctfl7jmturu+S9j7Fhx77T1fPglY/Ecpy
3WO0lRXOr3FzrEOv7jv/AFN/KDWPHps2Tw5yjNqtPh/aJ+Eb/wAOtw3jnD2q
O+SVchcyqUfeNI1DDQhF76/6vy0ra6ft1Kb7eqWl1XeMl52U9FY+vNu+PYOD
CYxMdbuXgid2iLe8FdywU+3TXTWrmHFGOvTHBR1WpnNfrnjOxsqyqxQKBQKB
QKBQeV38Bvh/7vufjXm3B7px8eSNi/ht/wAb/q+7+NeacuTJm4/tzTqyMBQK
BQf/2Q==


------解决方案--------------------
//一张MSDN字样的图
FileStream vFileStream = new FileStream(@ "c:\temp\temp.jpg ",
FileMode.OpenOrCreate, FileAccess.Write);
byte[] vBuffer = Convert.FromBase64String(richTextBox1.Text);
vFileStream.Write(vBuffer, 0, vBuffer.Length);