Base-64 문자 그룹이나 문자열의 길이가 올바르지 않은 문제 해결 방안

13716 단어
프로젝트의 특수한 수요에 따라 ActiveX 3차원 컨트롤러를 호출하여 컨트롤러의 특정한 부위를 캡처하는 작업을 합니다. 이 캡처 저장은 ActiveX 컨트롤러가 로컬에 저장하는 데 문제가 없습니다. 현재 이 캡처를 서버에 업로드하고 여러 사람이 공유하면 로컬 파일을 읽어야 합니다... 소통한 후에 ActiveX 컨트롤러 측은 인터페이스를 제공하여 관련 캡처 파일의 Base64 인코딩 문자열을 되돌려줍니다.웹 응용 프로그램에서 해당 형식으로 변환하여 서버에 저장하면 됩니다.
문제 발생:
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 문자열과 파일 사이의 상호 변환 작업을 제공합니다./// /// Base64 /// public class Base64Helper { /// /// Base64 /// /// Base64 /// public static void ConvertBase64ToFile(string base64, string filename) { //base64 = "/9j/4AAQSkZJRgABAQA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KslAaKdx3GaHoGj+G9Mh0fQtMhtLWCNUhggjCqqgADgewA/CrqqT9KFXJqVEHpU6JCEVOPauX0fWW+I3iRNW0HVroeHtLKtHLFax/ZtXnZX+7I+Wkii/duHjCqzkYkfYyibxJ4hk1zVf+EB8JX9z9sFwiazfad5R/smIx+b+8Z8qsjrsVUCs+JVfaF+cbEUWkeCfCiwIbkWGkaeFBd5rqYQxR9yd8sz7V6nc7H1JqG7sDmfiBb6w/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); } /// /// Base64 /// /// /// 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; } }
다음으로 전송:https://www.cnblogs.com/fonour/p/5857313.html

좋은 웹페이지 즐겨찾기