有人可以提供一些关于如何做到这一点?我可以为常规文本或字节数组执行此操作,但不知道如何处理PDF。我是否首先将pdf填充到一个字节数组中?Base64在C#中编码PDF?
回答
使用File.ReadAllBytes
加载PDF文件,然后使用Convert.ToBase64String(bytes)
正常编码字节数组。
Yikes!这可能会变大。 – 2009-01-24 03:34:43
确实。 但是现在机器有很多内存。如果有必要,从文件中读取缓冲区块是一项非常标准的技术:) – 2009-01-24 04:52:19
有一种方式,你可以做到这一点块,这样你就不必一次烧掉大量的内存。
.Net包含一个编码器,可以完成分块,但它有种奇怪的地方。他们把它放在System.Security.Cryptography命名空间中。
我已经测试了下面的示例代码,并且使用我的方法或上面的Andrew的方法得到了相同的输出。
以下是它的工作原理:启动一个名为CryptoStream的类。这是一种插入另一个流的适配器。您将一个名为CryptoTransform的类插入到CryptoStream中(它依次连接到您的文件/内存/网络流),并在数据从数据流读取或写入数据流时对数据执行数据转换。
通常情况下,转换是加密/解密,但.net包括ToBase64和FromBase64转换,所以我们不会加密,只是编码。
这是代码。我列出了Andrew的建议(也许命名不清),以便您可以比较输出。
class Base64Encoder
{
public void Encode(string inFileName, string outFileName)
{
System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.ToBase64Transform();
using(System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName),
outFile = System.IO.File.Create(outFileName))
using (System.Security.Cryptography.CryptoStream cryptStream = new System.Security.Cryptography.CryptoStream(outFile, transform, System.Security.Cryptography.CryptoStreamMode.Write))
{
// I'm going to use a 4k buffer, tune this as needed
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inFile.Read(buffer, 0, buffer.Length)) > 0)
cryptStream.Write(buffer, 0, bytesRead);
cryptStream.FlushFinalBlock();
}
}
public void Decode(string inFileName, string outFileName)
{
System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.FromBase64Transform();
using (System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName),
outFile = System.IO.File.Create(outFileName))
using (System.Security.Cryptography.CryptoStream cryptStream = new System.Security.Cryptography.CryptoStream(inFile, transform, System.Security.Cryptography.CryptoStreamMode.Read))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = cryptStream.Read(buffer, 0, buffer.Length)) > 0)
outFile.Write(buffer, 0, bytesRead);
outFile.Flush();
}
}
// this version of Encode pulls everything into memory at once
// you can compare the output of my Encode method above to the output of this one
// the output should be identical, but the crytostream version
// will use way less memory on a large file than this version.
public void MemoryEncode(string inFileName, string outFileName)
{
byte[] bytes = System.IO.File.ReadAllBytes(inFileName);
System.IO.File.WriteAllText(outFileName, System.Convert.ToBase64String(bytes));
}
}
我也在玩附加CryptoStream的地方。在Encode方法中,我将它附加到输出(写入)流,所以当我实例化CryptoStream时,我使用它的Write()方法。
当我阅读时,我将它附加到输入(阅读)流,因此我使用CryptoStream上的读取方法。附加到哪个流并不重要。我只需将合适的Read或Write枚举成员传递给CryptoStream的构造函数即可。
- 1. Base64在Google Chrome中编码为pdf?
- 2. C# - Base64编码JPEG到Base64编码BMP
- 3. C#:Base64编码
- 4. Objective-C Base64编码
- 5. 在本机pdf查看器中打开Base64编码的PDF
- 6. 编码base64在C#解码在Java
- 7. 在浏览器中解码Base64编码的PDF内容
- 8. 需要base64在c编码/解码
- 9. 将base64编码转换为pdf
- 10. PHP base64编码一个pdf文件
- 11. PHP:保存base64编码的PDF文件
- 12. 如何显示base64编码的pdf?
- 13. SymmetricAlgorithm DES C#。 Base64编码
- 14. C++ base64编码 - PHP兼容
- 15. C#到Ruby sha1 base64编码
- 16. Base64编码为base64,
- 17. 在Powershell中的Base64编码
- 18. 在Java中编码base64并在C#中解码
- 19. 在Java中编码base64和在C#中解码
- 20. 如何将在response.outputStream中生成的PDF转换为Base64编码
- 21. 在ReportLab生成的PDF中包含base64编码的图像
- 22. 在Java中对PDF进行Base64编码,在PL/SQL中解码:不起作用
- 23. C#Base64编码/解码失败
- 24. Base64编码解码
- 25. Base64在Coffeescript中的编码/解码
- 26. Cookie编码Base64
- 27. Base64编码
- 28. Backbone base64编码
- 29. 如何在Cocoa中编码和解码文件为Base64/Objective-C
- 30. 从javascript中触发打印预览的base64编码的PDF
为什么PDF应该与字节数组不同? – 2009-01-24 03:32:12