【问题标题】:Convert binary data to a pdf file将二进制数据转换为 pdf 文件
【发布时间】:2012-12-16 04:39:22
【问题描述】:

我正在尝试将二进制数据转换为其原始格式“.PDF”,但我不喜欢这两种解决方案。第一个是一个小文件,它会创建一个 PDF 文件,但它看起来是空的。第二个也创建了一个 PDF 文件,但我无法打开它。哪里出错了?

第一个代码:

Conn.Open();
SqlCommand cmd = Conn.CreateCommand();
cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo ='" + id + "')";
byte[] binaryData = (byte[])cmd.ExecuteScalar();

string s = Encoding.UTF8.GetString(binaryData);

File.WriteAllText("algo.pdf", s);

第二个代码:

Conn.Open();
SqlCommand cmd = Conn.CreateCommand();
cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo ='" + id + "')";
byte[] binaryData = (byte[])cmd.ExecuteScalar();

// Convert the binary input into Base64 UUEncoded output.
string base64String;
try
{
    base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
}
catch (System.ArgumentNullException)
{
    MessageBox.Show("Binary data array is null.");
    return;
}

cmd.CommandText = "Select Titulo From Artigo WHERE (IDArtigo ='" + id + "')";
string titulo = (string)cmd.ExecuteScalar();

// Write the UUEncoded version to the output file.
System.IO.StreamWriter outFile;
try
{
    outFile = new StreamWriter(titulo + ".pdf", false, System.Text.Encoding.ASCII);
    outFile.Write(base64String);
    outFile.Close();
}
catch (System.Exception exp)
{
    System.Console.WriteLine("{0}", exp.Message);
}

【问题讨论】:

  • 如果你想从字节数组创建PDF,你可以看看this

标签: c#


【解决方案1】:

您正在将文件写入文本,但您应该写入原始字节。 .PDF 文件是二进制文件,而不是文本文件,因此实际上,您在第一个代码示例中填充了错误的数据。

试试

    Conn.Open();
    SqlCommand cmd = Conn.CreateCommand();
    cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo = @id)";
    cmd.Parameters.AddWithValue("@id", id);
    byte[] binaryData = (byte[])cmd.ExecuteScalar();
    File.WriteAllBytes(("algo.pdf", binaryData);
    string s = Encoding.UTF8.GetString(binaryData);

System.IO.File.WriteAllBytes 记录在http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx,如果您还有其他问题。

【讨论】:

  • 这是我在多个问题和搜索中找到的最佳答案。我只想建议参数化这个命令文本,而不是连接一个字符串。即使您的 id 变量设置为 int 并且您不关心安全性,为了保持一致性,它仍然是一个好习惯。
【解决方案2】:

使用原始数据 (binaryData) 尝试 File.WriteAllBytes,不要尝试将其转换为其他任何内容

【讨论】:

    【解决方案3】:
    string sql = "select Lsn_File from Lessons_tbl where Lsn_name = '"TextBox1.Text + "'";
    
            cmd = new SqlCommand(sql, dal.cn());
    
            dal.Open();
            SqlDataReader dr = cmd.ExecuteReader();
    
             dr.Read();
             if (dr.HasRows)
               {             
                  byte[] lesson = (byte[])(dr[0]);
                  spathfile = System.IO.Path.GetTempFileName();
                  //move from soures to destination the extension until now is .temp
                 System.IO.File.Move(spathfile, 
                  System.IO.Path.ChangeExtension(spathfile,".pdf"));
                    //make it real pdf file  extension .pdf
                  spathfile = System.IO.Path.ChangeExtension(spathfile, ".pdf");
                   System.IO.File.WriteAllBytes(spathfile, lesson );
    
                   this.axAcroPDF1.LoadFile(spathfile);
             dal.close();
    

    【讨论】:

    • 请不要提供"select Lsn_File from Lessons_tbl where Lsn_name = '"TextBox1.Text + "'";之类的代码,因为这容易受到sql注入。
    • @Mathias:您可能想查看this meta question,了解如何编辑答案中的安全漏洞。
    • @BDL:在链接的问题中它说:An edit that fixes a potential security flaw in an answer, is a good edit.。我要求编辑,但被拒绝了。如果我有什么不对劲的话,我很抱歉。这个应该怎么处理?
    • 应该说得更清楚:由于您的一项编辑,我打开了元问题。这是一个持续的讨论,我想让你选择参与。
    • 哦,抱歉没看到。
    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2014-07-30
    • 2016-01-22
    • 1970-01-01
    • 2011-12-29
    • 2014-05-11
    • 2023-01-07
    相关资源
    最近更新 更多