0

i m currently working on project doing file management, and i just finish the code for upload file into my database(.mdf) table, however i have no idea how to download the file back to my folder(or anywhere else). I see people do download from ASP.Net, OR web sql database, but i wish if someone could tell me how to download it from a mdf database.

private void find_Click(object sender, EventArgs e)
{
  OpenFileDialog dlg = new OpenFileDialog();
  if (dlg.ShowDialog() == DialogResult.OK)
  {
   string Path = dlg.FileName.ToString();
   PathBox.Text = Path;
   PicBox.location = Path;
  }
}

private void save_Click(object sender, eventargs e) 
{ 
  byte[] Doc = null;
  Filestream fs = new FileStream(this.PathBox.Text, FileMode.Open, FileAccess.Read);
  BinaryReader br = new BinaryReader(fs);
  Doc = br.ReadBytes((int)fs.Length);

  string DS = "datasource = localDB ......";
  string Insertcmd = "insert into ......";
  MySqlConnection conn = new MySqlConnection(DS);
  MySqlCommand cmd = new MySqlCommand(Insertcmd, conn);
  MySqlDataReader msdr;

  try
  {
      conn.Open();
      cmd.Parameter.Add(new MySqlParameter("@Document", Doc));

      msdr = cmd.ExecuteReader();
      MessageBox.Show("file saved");
      While (msdr.Read())
      {
      }
   catch (Exception ex)
    {
    }
}

could someone give me a code that is able to download from .mdf database? or even any hint will be very helpful

thanks

  • Select the file from the database and write the byte array to disk, something like : http://stackoverflow.com/questions/381508/can-a-byte-array-be-written-to-a-file-in-c Search for 'write byte array to disk'. – GrandMasterFlush Sep 29 '15 at 09:15
  • @GrandMasterFlush thank you very much, that really helps a lot :) – UnseenRoad Sep 29 '15 at 13:01
  • @GrandMasterFlush Hi, i have a question, could you please check this link: http://stackoverflow.com/questions/32939079/value-cannot-be-null-parameter-name-bytes-suggestion-please – UnseenRoad Oct 05 '15 at 07:22

1 Answers1

0

HI It seems that you are saving the file in a binary format (BLOB ) object ? So when you want to show the image after getting from Database you could read it using a memory stream and show it. or maybe use context.Response.BinaryWrite() to output the image.

http://csharpdotnetfreak.blogspot.com/2009/07/display-images-gridview-from-database.html might be helpful.

m5c
  • 567
  • 8
  • 13
  • thanks for reply, i m not sure if i m saving it as BLOB -- still learning how to use C#, but mostly this is for PDF document and Word documents, which will contain both text and pictures. I will try your code to see if i can output the file :p – UnseenRoad Sep 29 '15 at 13:04