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:
IhaveafupCPF
fieldthatreceivestheimage,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?