Direct image upload in MySQL DB using C #

1

I'm creating a WEB application using C # and a MySQL DB. I need to upload an image to the Bank and then retrieve it.

I am a beginner in this world, but what I have so far:

IhaveafupCPFfieldthatreceivestheimage,thenIexecutetheInsertbelow:

stringcomando="SELECT imagem_cpf FROM tb_cpf WHERE id_usuario=@loginUsuario";
MySqlCommand cmd = new MySqlCommand(comando, mConn);

string comando = "INSERT INTO tb_cpf (id_usuario, numero, imagem_cpf) VALUES (@id_usuario, @numero, @imagem_cpf)";
MySqlCommand cmd = new MySqlCommand(comando, conexao);
//preenchimento dos parâmetros
cmd.Parameters.AddWithValue("@id_usuario", emailUsuario);
cmd.Parameters.AddWithValue("@numero", txtCodCPF.Text.ToString());

byte[] imageBytes = new byte[fupCPF.PostedFile.InputStream.Length + 1];
fupCPF.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);

cmd.Parameters.AddWithValue("@imagem_cpf", imageBytes);
conexao.Open();
cmd.ExecuteNonQuery();
conexao.Close();

lblImgCPFErro.Text = "INSERIDO COM SUCESSO!!";

I'm going to put something in the database (a .bin file) but I do not know how to test if it's working ... Could anyone help me?

I created a page for viewing, page_Load has the following code (after connecting to the Bank)

string comando = "SELECT imagem_cpf FROM tb_cpf WHERE id_usuario=@loginUsuario";
MySqlCommand cmd = new MySqlCommand(comando, mConn);

//preenchimento dos parâmetros
cmd.Parameters.AddWithValue("@loginUsuario", loginUsuario);
mConn.Open();

MySqlDataReader myReader = cmd.ExecuteReader();

if (myReader.Read())
{
    Response.ContentType = "image/jpeg";
   Response.BinaryWrite((byte[])myReader["imagem_cpf"]);

}

myReader.Close();
mConn.Close();

Can someone give me a light?

    
asked by anonymous 08.04.2016 / 15:55

1 answer

0

I do this in Folder normally not to overload the database, although I have already done this and in case I need to search in my old projects.

Below is an excerpt from the source I upload to my current project

if (Request.Files["Arquivo" ] != null && Request.Files["Arquivo" ].FileName != "")
            {
                veiculoOcorrenciaAnexoViewModel.NomeArquivo = Path.GetFileName(Request.Files["Arquivo" ].FileName);
                string path = Request.PhysicalApplicationPath + "\Arquivos\Veiculos\Ocorrencias\" + veiculoOcorrenciaAnexoViewModel.VeiculoOcorrenciaId + "\" ;
                if (!System.IO.Directory.Exists(path))
                    System.IO.Directory.CreateDirectory(path);

                Request.Files["Arquivo" ].SaveAs(path + veiculoOcorrenciaAnexoViewModel.NomeArquivo);
                _vOcorrenciaAnexoAppService.Adicionar(veiculoOcorrenciaAnexoViewModel);    
            }
    
08.04.2016 / 16:26