I am trying to convert binary data to the original ".PDF" format, but any of the solutions that I have are. The first is small, it creates a PDF file, but it seems empty. The second also creates a PDF file, but I cannot open it. Where is the mistake?
First code:
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);
Second code:
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); }
source share