将base64Binary转换为pdf

Nov*_*per 12 c# pdf binary

我有base64Binary的原始数据.

string base64BinaryStr = "J9JbWFnZ......"
Run Code Online (Sandbox Code Playgroud)

我怎样才能制作pdf文件?我知道它需要一些转换.请帮我.

Mus*_*sis 30

第1步是从base64字符串转换为字节数组:

byte[] bytes = Convert.FromBase64String(base64BinaryStr);
Run Code Online (Sandbox Code Playgroud)

第2步是将字节数组保存到磁盘:

System.IO.FileStream stream = 
    new FileStream(@"C:\file.pdf", FileMode.CreateNew);
System.IO.BinaryWriter writer = 
    new BinaryWriter(stream);
writer.Write(bytes, 0, bytes.Length);
writer.Close();
Run Code Online (Sandbox Code Playgroud)

  • 想知道为什么这会写入 pdf 但不编码图像。我的 pdf 打不开。 (2认同)

Ral*_*ine 15

using (System.IO.FileStream stream = System.IO.File.Create("c:\\temp\\file.pdf"))
{
    System.Byte[] byteArray = System.Convert.FromBase64String(base64BinaryStr);
    stream.Write(byteArray, 0, byteArray.Length);
}
Run Code Online (Sandbox Code Playgroud)


dus*_*88c 8

首先将Bas64字符串转换为byte[]并写入文件。

byte[] bytes = Convert.FromBase64String(base64BinaryStr); 
File.WriteAllBytes(@"FolderPath\pdfFileName.pdf", bytes );
Run Code Online (Sandbox Code Playgroud)


小智 5

此代码不会在硬盘驱动器上写入任何文件。

Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Length", base64Result.Length.ToString());
Response.AddHeader("Content-Disposition", "inline;");
Response.AddHeader("Cache-Control", "private, max-age=0, must-revalidate");
Response.AddHeader("Pragma", "public");
Response.BinaryWrite(Convert.FromBase64String(base64Result));
Run Code Online (Sandbox Code Playgroud)

注意:该变量base64Result包含 Base64 字符串:“JVBERi0xLjMgCiXi48/TIAoxI...”


Mik*_*ark 4

您需要做的就是通过任何 Base64 解码器运行它,该解码器会将您的数据作为字符串并传回字节数组。然后,只需将文件名中包含 pdf 的文件写出即可。或者,如果您将其流式传输回浏览器,只需将字节写入输出流,并在标头中标记适当的 mime 类型。

大多数语言都有内置的方法来转换为 Base64 或从 Base64 转换。或者,使用您的特定语言进行简单的 Google 搜索将返回您可以使用的大量实现。来回转换为 Base64 的过程非常简单,即使是新手开发人员也可以实现。