Click here to Skip to main content
12,626,216 members (32,894 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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::
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 3-Jan-13 23:49pm
Updated 4-Jan-13 0:12am
v6

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100