項目特殊需要,調用ActiveX三維控件進行控件某一特殊部位的截圖操作,這個截圖保存由ActiveX控件控制保存到本地是沒問題的,現在需要將這個截圖上傳到服務器,多人共享,就牽扯到需要讀取本地文件……後溝通後,ActiveX控件方提供一個接口,返回相關截圖文件的Base64編碼字符串,由Web應用程序轉換為對應格式,存儲在服務器即可。
遇到問題:
Base-64 字符數組或字符串的長度無效
直接調用Convert.FromBase64String方法時,報“Base-64 字符數組或字符串的長度無效”的錯誤。
MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64));
於是想到對ActiveX返回的Base64字符串進行編碼操作
base64 = System.Web.HttpUtility.HtmlEncode(base64);
MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64));
運行程序,報“輸入的不是有效的 Base-64 字符串,因為它包含非 Base-64 字符、兩個以上的填充字符,或者填充字符間包含非法字符”錯誤。
最終解決方案:
對Base64字符串進行特殊字符替換,並進行PadRight操作,解決問題
string dummyData = base64.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+"); if (dummyData.Length % 4 > 0) { dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '='); } MemoryStream stream = new MemoryStream(Convert.FromBase64String(dummyData));
最後提供一下封裝的Base64Helper類,提供Base64字符串與文件之間相互轉換操作。
/// <summary> /// Base64編碼字符串與文件轉換幫助淚 /// </summary> public class Base64Helper { /// <summary> /// Base64編碼字符串轉文件 /// </summary> /// <param name="base64">Base64編碼字符串</param> /// <param name="filename">文件完整路徑</param> public static void ConvertBase64ToFile(string base64, string filename) { //base64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCABkAGQDASIAAhEBAxEB/8QAHwAA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z7V6nc7H1JqG7sDmfiBb6w/jvRn8LTG31efRtRtLK/uNNlubW1Dy2UkjzKjKM7Yfk3MoLYGeTVG2h8P+H/ABYPCWjWttoXhDwZpZv9UxGbe0a5lZpEDtJb+U4jVZLh3ScOjvE8ifMrVctUuNJ0bXfjRrvhuGTVorK8fS4byE280Fgihkt2cvL5QlMKSvtC8su+MNHiuf8ADZk1/wCH3gb4U6f4is7ybVdFttV8RajoniOa+jltAqSSSQ3ZuobqSO4nYLHcbZA6b96gNioe4zpfhBb6jqtjffEvWWnW48T3C3NtbSXDMtrYqu22jCrdXEIYx/vGaEqrtIWKg5rsK5XUfiKmn/Gix+HFxdIqXejGaKEIAzzM0rKSxPICW02AvPUkYwR1pUHtVITIyoNN2HOKkKkUlVdgROn50xlB4NTkAio3Qn61SdwK7RjPNFS0UwJVAyBWL8UPE9/4I+GXiPxppMMMl1o+g3l9bR3CkxtJFC8ihgpBK5UZAIOO4ooqJASfD/RYtO0MavJe3V1eauEvL+5u5y7PI0aABRwsaKqgBECrwWwWZmbB+LD2+teNvB3w81iwiutL1m8vW1G2lZ9k6w2rkRSIG2SxtuO6ORWU4U4BUEFFR9lD6neIoPWvMf2Wb1NT8P64ws1t10nWF0OyghnlMUdpaW8SQqsbuyRnDsW2BQxOSKKKH8SHHY5D4jEj/goZ4AHr4Xn/APRWoV7+VB60UUR3ZQwjPBqM8HFFFUZhSOOM0UU1uBGVB5IoooqwP//"; string dummyData = base64.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+"); if (dummyData.Length % 4 > 0) { dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '='); } MemoryStream stream = new MemoryStream(Convert.FromBase64String(dummyData)); //filename = @"C:\Users\admin\Desktop\Tongji\test.jpg"; Bitmap img = new Bitmap(stream); img.Save(filename); } /// <summary> /// 文件轉Base64編碼字符串 /// </summary> /// <param name="fileName">文件完整路徑</param> /// <returns></returns> public static string ConvertFileToBase64(string fileName) { //fileName = @"C:\Users\admin\Desktop\Tongji\test.jpg"; FileStream fs = File.OpenRead(fileName); BinaryReader br = new BinaryReader(fs); byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length)); string base64String = Convert.ToBase64String(bt); return base64String; } }