【问题标题】:How can I correct an update when uploading an image to asp.net mvc5?将图像上传到 asp.net mvc5 时如何更正更新?
【发布时间】:2021-07-23 03:54:48
【问题描述】:

这是我使用 sql server 的数据库,我使用 image 属性保存我的图像:

 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("<script>alert('Perfil actualizado')</script>");
                return View("../Home/Perfil");
           }
            catch (Exception es)
            {
              
                con.Close();
                Response.Write("<script>alert('Perfil no actualizado')</script>");
                return View("Actualizarperfil");

                throw es;
                // con.Close();

            }
        }

我命令像这样调用我的图像属性

          <form action="Updateprofile" method="post" class="form-horizontal" role="form">
       

                 <div class="form-group">
                    <label for="Imagen" class="col-sm-3 control-label">Imagen</label>
                    <div class="col-sm-9">
                        <input type="file" name="Imagen" required />
                    </div>
                </div>

                <button type="submit" class="btn btn-primary btn-block">Actualizar datos</button>
 </form>

但是当我点击更新按钮时,它会显示这个错误:

The input is not a valid Base-64 string as it contains a non-base 64 character

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

【问题讨论】:

  • 这是一个正在提交的 HTML 表单,因此它将作为 FormData 发送。服务器需要从请求中提取文件,因此有 IFormFile 或 HttpPostedFileBase 等接口。请检查这是否适用:stackoverflow.com/questions/22623947/…

标签: c# asp.net sql-server asp.net-mvc c#-4.0


【解决方案1】:

我认为您想使用Convert.ToBase64String 作为您的图像。该函数的文档可以在here找到。

此外,连接字符串以创建 SQL 命令会导致 SQL 注入漏洞。你应该切换到prepared statement。

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 2019-02-20
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多