1
I am creating a WEB application using C# and a Mysql BD. I need to upload an image to the bank and then retrieve it.
I’m a beginner in this world, but what I have so far:
I have a field fupCPF
which receives the image, then run Insert below:
string comando = "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 insert something into the bank (a .bin file) but I don’t know how to test if it’s working... Could someone help me?
I created a page for viewing, page_Load has the following code (after connecting to the Database)
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();
Someone can give me a light?
Is it necessary to save the image to the database? I would save it to a folder inside the server and only save the image path.
– Tiedt Tech
Good morning Marlon, it could be yes. but I have no idea how to do that...
– Walter Junior