Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 22:49pm
Edited 3-Jan-13 23:12pm
v6

1 solution

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

  Print Answers RSS
0 Maciej Los 469
1 OriginalGriff 379
2 CPallini 205
3 Sergey Alexandrovich Kryukov 170
4 Sharmanuj 109
0 OriginalGriff 5,649
1 Sergey Alexandrovich Kryukov 4,783
2 Peter Leow 2,954
3 Maciej Los 2,689
4 DamithSL 2,465


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