首页 > 解决方案 > 如何将数组 [] 转换为 HttpPostedFileBase

问题描述

我从数据库中获取信息,在这些信息之间有姓名、日期和图像,我想在表格中显示这些信息,并通过 ajax 以 json 格式发送信息。

这里执行查询并将信息带入数据库的方法,观察到的新列表是由于要保存已转换为 HttpPostedFileBase 的照片字段

public async Task<JsonResult> GetEmpleados(HttpPostedFileBase Foto)
    {
        List<spr_GetEmpleados> listaEmpleados = new List<spr_GetEmpleados>();
        listaEmpleados.Clear();
        using (var db = new AppDbContext())
        {
            listaEmpleados = await db.Set<spr_GetEmpleados>().SqlQuery("EXEC spr_GetEmpleados").ToListAsync();
            var newlist = listaEmpleados.Select(x => new
            {
                idEmpleado=x.Id,
                Nombres=x.Nombres,
                Apellidos=x.Apellidos,
                Correo=x.Correo,
                FechaRegistro=x.FechaRegistro,
                FechaNacimiento=x.FechaNacimiento,
                Departamento = x.Departamento,
                Foto=Imagen.ConvertImage(x.Foto)
            });
            db.Dispose();
            var jsonResult = Json(new { success = true, data = newlist.ToList() }, "application/json",
           System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }
    }

在这种方法中会发生转换,我将转换为 Image 类型只是为了测试,但想法是将其更改为 HttpPostedFileBase 类型

public static Image ConvertImage(byte[] Foto)
{
    using (MemoryStream memoryStream = new MemoryStream(Foto))
    {
        var foto = Image.FromStream(memoryStream);
        return foto;
    }
}

标签: c#ajaxasp.net-mvc

解决方案


推荐阅读