2015-08-24 33 views
1

我顯示圖像作爲基礎64從保存在sql數據庫中的字節數組。圖像來源與基地64沒有顯示使用Angular JS

這是我的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" /> 

圖片但是不表示二進制數與圖像源附加在圖像

這裏看到的是,我用於保存圖像作爲代碼字節數組

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


      } 
    } 

這裏是用於獲取圖像

代碼
Avatar = Convert.ToBase64String(profile.avatar), 

圖片顯示不出來,不過串與sourcce

enter image description here

需要幫助的是什麼,我做錯了追加?

+0

侑數據不看像base64編碼的字符串 –

+0

這樣做:在你的控制器'user.Avatar ='data:image/jpeg; base64,'+ user.Avatar;'然後在html'data-ng-src =「{{user.Avatar}} 「' –

+0

@gauravbhavsar看到編輯 –

回答

2

你的數據不是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; 
    } 
} 
+0

我有圖像作爲字節數組從httppostedfilebase –

+0

所以,你需要這樣做: 字符串base64String = Convert.ToBase64String(imageBytes ); 然後你將有base64,你需要返回到客戶 – David

+0

看到編輯請 –