I would like to know how to save an image in the MySQL database, the table is called db_usuarios with the following structure:
ID int
GrupoUsuario int
Nome Varchar
Apelido Varchar
....
Foto Blob
UserController Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Cadastro(FormCollection inputs, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
MD5 md5Hasher = MD5.Create();
byte[] valorCriptografado = md5Hasher.ComputeHash(Encoding.Default.GetBytes(inputs["EdtSenha"]));
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < valorCriptografado.Length; i++)
{
stringBuilder.Append(valorCriptografado[i].ToString("x2"));
}
DBConexao BDConexao = new DBConexao();
using (MySqlConnection conexao = BDConexao.Conectar())
{
StringBuilder QueryInsert = new StringBuilder();
QueryInsert.AppendLine("INSERT INTO DB_USUARIOS (GRUPOUSUARIO, NOME, APELIDO, SENHA, ");
QueryInsert.AppendLine("ALTERARSENHA, SITUACAO, EMAIL, SERVIDORSMTP, PORTA, USUARIOEMAIL, SENHAEMAIL, EMAILTLS, EMAILSSL, FOTO) ");
QueryInsert.AppendLine("VALUES (@GRUPOUSUARIO, @NOME, @APELIDO, @SENHA, @ALTERARSENHA, ");
QueryInsert.AppendLine("@SITUACAO, @EMAI L, @SERVIDORSMTP, @PORTA, @USUARIOEMAIL, @SENHAEMAIL, @EMAILTLS, @EMAILSSL, @FOTO) ");
QueryInsert.ToString();
MySqlCommand comando2 = new MySqlCommand(QueryInsert.ToString(), conexao);
comando2.Parameters.AddWithValue("@GRUPOUSUARIO", inputs["GRUPOUSUARIO"]);
comando2.Parameters.AddWithValue("@NOME", inputs["EdtNome"]);
comando2.Parameters.AddWithValue("@APELIDO", inputs["EdtApelido"]);
comando2.Parameters.AddWithValue("@SENHA", stringBuilder.ToString());
comando2.Parameters.AddWithValue("@ALTERARSENHA", inputs["AlterarSenha"]);
comando2.Parameters.AddWithValue("@EMAIL", inputs["EdtEmail"]);
comando2.Parameters.AddWithValue("@SITUACAO", inputs["Situacao"]);
comando2.Parameters.AddWithValue("@SERVIDORSMTP", inputs["EdtServidorSMTP"]);
comando2.Parameters.AddWithValue("@PORTA", inputs["EdtPorta"].ToString());
comando2.Parameters.AddWithValue("@USUARIOEMAIL", inputs["EdtUsuarioEmail"]);
comando2.Parameters.AddWithValue("@SENHAEMAIL", inputs["EdtSenhaEmail"]);
comando2.Parameters.AddWithValue("@EMAILTLS", inputs["EmailTLS"]);
comando2.Parameters.AddWithValue("@EMAILSSL", inputs["EmailSSL"]);
comando2.Parameters.AddWithValue("@FOTO", inputs["EdtFoto"]);
conexao.Open();
comando2.ExecuteNonQuery();
ViewBag.GRUPOUSUARIO = new SelectList
(
new GrupoUsuarioModel().ListaGrupoUsuario(),
"ID",
"NOME"
);
conexao.Close();
return RedirectToAction("Index", "Usuario");
}
}
return View();
}
The template class is in the following form:
namespace Promessas.Models
{
public class UsuarioModel
{
[Key]
public int ID { get; set; }
public GrupoUsuarioModel GrupoUsuario { get; set; }
[Index("UserNameIndex", IsUnique = true)]
public String Nome { get; set; }
[Required]
[MaxLength(15)]
[Index("UserNameIndex",IsUnique = true)]
public String Apelido { get; set; }
[DataType(DataType.Password)]
public String Senha { get; set; }
public String AlterarSenha { get; set; }
public String Situacao { get; set; }
public String Email { get; set; }
public String ServidorSMTP { get; set; }
public int Porta { get; set; }
[DataType(DataType.EmailAddress)]
public String UsuarioEmail { get; set; }
[DataType(DataType.Password)]
public String SenhaEmail { get; set; }
public String EmailTLS { get; set; }
public String EmailSSL { get; set; }
public byte[] Foto { get; set; }
}
}
The view is as follows:
<input type="file" name="__file" id="__file" accept="image/*">