当将图像上传到asp.net mvc5时,如何纠正更新问题?

6 浏览
0 Comments

当将图像上传到asp.net mvc5时,如何纠正更新问题?

这是我使用SQL Server的数据库,我使用图像属性保存我的图像:

 create table registro(id int IDENTITY(1,1),Nombre varchar(100),appaterno varchar(100),apmaterno varchar(100),lineainvestigacion varchar(100),correo varchar(100),correoi varchar(100),telefonop int,extension int,telefonoof int, usuario varchar(100) PRIMARY KEY ,clave varchar(100),imagen image,idrol varchar(100),ciudadr varchar(100));

这是我的类:

public class Registro1
{
    public int id { get; set; }
    public string nombre { get; set; }
    public string appaterno { get; set; }
    public string apmaterno { get; set; }
    public string correo { get; set; }
    public string usuario { get; set; }
    public string clave { get; set; }
  // public string Imagen { get; set; }
    public byte[] imagen { get; set; }
    public string idrol { get; set; }
    public string lineainvestigacion { get; set; }
    public string correoi { get; set; }
    public int telefonoof { get; set; }
    public double telefonop { get; set; }
    public int extension { get; set; }
    public string ciudadr { get; set; }
}

这是我的控制器:

public ActionResult Updateprofile(Registro1 rg)
{
    connectionString();
    con.Open();
    com.Connection = con;
    com.CommandText =
 "update registro set Nombre='" + rg.nombre + "',ciudadr='" + rg.ciudadr + "',correoi='" + rg.correoi 
 + "',correo='" + rg.correo + "',clave='" + rg.clave + "',lineainvestigacion='" + 
 rg.lineainvestigacion + "',telefonop='" + rg.telefonop + "',extension='" + rg.extension + 
 "',telefonoof='" + rg.telefonoof + "',imagen='" + Convert.ToByte(rg.imagen) + "' WHERE usuario = '" 
 + rg.usuario + "'";
    try
    {
        dr = com.ExecuteReader();
        con.Close();
        Response.Write("");
        return View("../Home/Perfil");
   }
    catch (Exception es)
    {
        con.Close();
        Response.Write("");
        return View("Actualizarperfil");
        throw es;
        // con.Close();
    }
}

我用以下命令调用我的图像属性

          

但是当我点击更新按钮时,它显示以下错误:

输入不是有效的Base-64字符串,因为它包含非Base-64字符

我想知道我在控制器中是否将字节属性转换错误,或者是否有其他错误。

0
0 Comments

问题原因:在上传图片到ASP.NET MVC5时,出现了更新错误。可能原因是未正确使用Convert.ToBase64String函数对图像进行转换。

解决方法:您应该使用Convert.ToBase64String函数对图像进行转换。可以在这里找到该函数的文档。

另外,使用字符串连接创建SQL命令会打开SQL注入漏洞。您应该切换到准备语句

0