Click here to Skip to main content
13,249,813 members (77,927 online)
Rate this:
Please Sign up or sign in to vote.
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::
public byte[] FileToByteArray(string _FileName) 
   byte[] _Buffer = null;
      // 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 
   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

1 solution

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

Solution 1


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 |
Web01 | 2.8.171114.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2017
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