2012-11-01 183 views
5

我正在使用MVC#。當我嘗試將圖像轉換爲base64字符串時,圖像未打開。我從JavaScript獲取base64字符串並將其轉換爲控制器中的圖像。這是我的代碼。將base64轉換爲c中的圖像#

var base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABG4AAAH0CAYAAABsCGvoAAAgAElEQVR4Xuy9C5RV1Zmo++96P3ZVURSv4hEEFdQobYKtJESjJME+6WgTpPUQIrfpY8NRui/aKlcvY2hON0fN5TIUbh/6XDs9bgah0+1BY+gk2p2QFpIAgk9AiGBhlQgIokgVRQFVu3bd9S+c21WbXVV71157Pfb+loNh1ar5+Oc3Z/H46p9zRnqsR3ggAAEIQAACEIAABCAAAQhAAAIQgAAEAkcggrgJ3JwQEAQgAAEIQAACEIAABCAAAQhAAAIQsAkgblgIEIAABCAAAQhAAAIQgAAEIAABCEAgoAQQNwGdGMKCAAQgAAEIQAACEIAABCAAAQhAAAKIG9YABCAAAQhAAAIQgAAEIAABCEAAAhAIKAHETUAnhrAgAAEIQAACEIAABCAAAQhAAAIQgADihjUAAQhAAAIQgAAEIAABCEAAAhCAAAQCSgBxE9CJISwIQAACEIAABCAAAQhAAAIQgAAEIIC4YQ1AAAIQgAAEIAABCEAAAhCAAAQgAIGAEkDcBHRiCAsCEIAABCAAAQhAAAIQgAAEIAABCCBuWAMQgAAEIAABCEAAAhCAAAQgAAEIQCCgBBA3AZ0YwoIABCAAAQhAAAIQgAAEIAABCEAAAogb1gAEIAABCEAAAhCAAAQgAAEIQAACEAgoAcRNQCeGsCAAAQhAAAIQgAAEIAABCEAAAhCAAOKGNQABCEAAAhCAAAQgAAEIQAACEIAABAJKAHET0IkhLAhAAAIQgAAEIAABCEAAAhCAAAQggLhhDUAAAhCAAAQgAAEIQAACEIAABCAAgYASQNwEdGIICwIQgAAEIAABCEAAAhCAAAQgAAEIIG5YAxCAAAQgAAEIQAACEIAABCAAAQhAIKAEEDcBnRjCggAEIAABCEAAAhCAAAQgAAEIQAACiBvWAAQgAAEIQAACEIAABCAAA...AgkKCA4CbBouoSAQIECBAgQIAAAQIECBAgkIaA4CaNOuoFAQIECBAgQIAAAQIECBAgkKCA4CbBouoSAQIECBAgQIAAAQIECBAgkIaA4CaNOuoFAQIECBAgQIAAAQIECBAgkKCA4CbBouoSAQIECBAgQIAAAQIECBAgkIaA4CaNOuoFAQIECBAgQIAAAQIECBAgkKCA4CbBouoSAQIECBAgQIAAAQIECBAgkIaA4CaNOuoFAQIECBAgQIAAAQIECBAgkKCA4CbBourS9wLLy8vhypUrob+/Pxw8eBALAQIECBAgQIAAAQIECBAonIDgpnAlS7vBY2Nj4e7du6GpqSkMDg4KXNIut94RIECAAAECBAgQIECAQBkBwY0hUjcC09PT4enTp9kMmdIxOzsbhoeHw/z8fOjp6QlDQ0Ohubk5XL16NbS3t4epqakwMjIS2trafvTavXv3ws2bN1e+Pjo6GmZmZkJLS8ua13R0dNSNh4YQIECAAAECBAgQIECAAAHBjTFQNwLj4+Ph2LFjq86y+XzZU29vb4ghzPHjx0NfX1/W/vj11V6LAc+FCxdCDHG6urqy82NANDk5GS5evPija+oGQ0MIECBAgAABAgQIECBAgMAnAcGNYVA3AqsFN4uLi9mSqbm5uZXlUzG4KQUypRkyMbj50ms3btzIlmCVjiNHjmTBzQ+vqRsMDSFAgAABAgQIECBAgAABAoIbY6CeBCYmJrKlUgMDAyvNiq/FmTJHjx4Nly9fDpcuXQp5gpvPZ9yUbr5a2FNPHtpCgAABAgQIECBAgAABAgTMuDEG6kqgtDlxbNTp06fD4cOHw7Vr17J9aU6dOhWWlpbCmTNnvnrGTdwDp7THTWnj49UCoLrC0BgCBAgQIECAAAECBAgQaHgBwU3DDwEABAgQIECAAAECBAgQIECAQL0KCG7qtTLaRYAAAQIECBAgQIAAAQIECDS8wP8AN7hUDozwDE4AAAAASUVORK5CYII="; 

byte[] str = System.Text.Encoding.Unicode.GetBytes(base64); 
var buffer = Convert.FromBase64String(Convert.ToBase64String(str)); 
var file = Server.MapPath("~/Content/images/err.png"); 
System.IO.File.WriteAllBytes(file, buffer); 
Image img = new Image(); 
img.ImageUrl = Server.MapPath("~/Content/images/err.png"); 

回答

5

我複製了base64字符串提示窗口。我用AJAX方法張貼從客戶端中的Base64字符串,它爲我工作

C#

[HttpPost] 
    public void Base64ToImage(string source) 
    { 
     string base64 = source.Substring(source.IndexOf(',') + 1); 
     base64 = base64.Trim('\0'); 
     byte[] chartData = Convert.FromBase64String(base64); 
} 

AJAX方法

var picture = jqplotToImg($('#ChartContent')); 
    //prompt("", picture); 

    $.ajax({ type: 'POST', 
     url: '../Report/Base64ToImage', 
     async: false, 
     data: { source: picture }, 
     success: function (data) { 
      //alert(data); 
     } 
    }); 
+0

一旦你有圖像(chartData)怎麼做你實際上保存在你的控制器的服務器上的某個地方,我試圖chartdata.SaveAs,但那不工作... – dave317

+0

@ dave317你的意思是,保存從base64字符串創建的文件? – Curiousity

20

您的代碼是完全錯誤的。

您需要通過刪除第一個逗號前的所有內容來解析出該字符串中的實際base64數據。

然後,您可以在該數據上撥打Convert.FromBase64String()

string source = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABG4...YII="; 
string base64 = source.Substring(source.IndexOf(',') + 1); 
byte[] data = Convert.FromBase64String(base64); 
+0

謝謝,我刪除,並試圖又一次同樣的錯誤 – DON

+0

_What錯誤你會得到嗎? – SLaks

+0

看我的編輯;我忘了刪除逗號本身 – SLaks