Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to save any type of file,choosen and save it to database in a column with its content converted to binary.
i am having problem with pdf file..while saving it to database,data gets currput.

this is how i convert the file to binary::
C#
public byte[] FileToByteArray(string _FileName) 
{
   byte[] _Buffer = null;
   try
   {
      // Open file for reading 
      System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName,    System.IO.FileMode.Open, System.IO.FileAccess.Read);
      // attach filestream to binary reader 
      System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
      // get total byte length of the file 
      long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
      // read entire file into buffer 
      _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);
      // close file reader 
      _FileStream.Close();
      _FileStream.Dispose();
      _BinaryReader.Close();
   }
   catch (Exception _Exception)
   {
      // Error 
      Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
   }
   return _Buffer;
}
Posted
Updated 3-Jan-13 23:12pm
v6

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900