简体   繁体   English

使用Angular JS无法显示base 64的图像源

[英]Image source with base 64 not showing up using Angular JS

I am displaying images as base 64 from a byte array saved in sql database . 我正在从sql数据库中保存的字节数组中将图像显示为base 64。

Here is my html code 这是我的HTML代码

      <img id="imgavatar" data-ng-src="data:image/jpeg;base64,{{user.Avatar}}" data-err-src="~/Images/avatar.png" style="width:100px;height:100px;position:absolute" />

    <img id="imgprofile" data-ng-src="data:image/jpeg;base64,{{user.ProfileImage}}" data-err-src="~/Images/image-icon.png" style="width:200px;height:200px;position:absolute" />

Image not showing however the binary numbers are appending with image source as seen in picture 图像未显示,但是二进制数字附加在图像源中,如图所示

Here is the code which I used to save the image as byte array 这是我用来将图像保存为字节数组的代码

[HttpPost]
    public ActionResult Edit(string id,string user,HttpPostedFileBase avatar, HttpPostedFileBase profile)
    {
     if (avatar != null)
            {
                p.avatar = new byte[avatar.InputStream.Length];


            }
    }

Here is the code for getting the image 这是获取图像的代码

Avatar = Convert.ToBase64String(profile.avatar),

Image not showing up however the string is appending with sourcce 图像未显示,但是字符串附加了源

在此处输入图片说明

Need help What I am doing wrong? 需要帮助我做错了什么?

Your data it's not Base64 data. 您的数据不是Base64数据。 Probably its problem in server side. 可能是服务器端的问题。

For Client: Try to use this plugin for angular: https://github.com/adonespitogo/angular-base64-upload 对于客户端:尝试将此插件用于角度: https : //github.com/adonespitogo/angular-base64-upload

For server side Here code for convert image to Base64: 对于服务器端,此处将图像转换为Base64的代码:

public string ImageToBase64(Image image, 
  System.Drawing.Imaging.ImageFormat format)
{
  using (MemoryStream ms = new MemoryStream())
  {
    // Convert Image to byte[]
    image.Save(ms, format);
    byte[] imageBytes = ms.ToArray();

    // Convert byte[] to Base64 String
    string base64String = Convert.ToBase64String(imageBytes);
    return base64String;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM