我試圖將二進制數據轉換爲其原始格式「.PDF」,但是其中任何一種解決方案都有助於我的hed。第一個是一個小的,它創建一個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);
}
如果你願意,你可以看看[this](http://stackoverflow.com/questions/3961804/itextsharp-creation-of-a-pdf-from-a-list-of-byte-arrays)想從字節數組中創建PDF。 – Tilak