Base-64 character array or invalid string length problem solution, base-64 Array
Special requirements of the project: Call the ActiveX 3D control to operate on a special part of the control. It is no problem to save the control from the ActiveX control to the local device. Now you need to upload the control to the server, shared by many people, it involves the need to read local files ...... After communication, the ActiveX control side provides an interface to return the Base64 encoded string of the relevant file, which is converted from the Web application to the corresponding format and stored on the server.
Problems:
The Base-64 character array or string length is invalid.
When the Convert. FromBase64String method is called directly,"The Base-64 character array or string length is invalid..
MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64));
So I want to encode the Base64 string returned by ActiveX.
base64 = System.Web.HttpUtility.HtmlEncode(base64);
MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64));
Run the program and report"The input is not a valid Base-64 string because it contains non-Base-64 characters, more than two filling characters, or contains invalid characters between filling characters."Error.
Final Solution:
Replaces the Base64 string with special characters and performs the PadRight operation to solve the problem.
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));
Finally, the encapsulated Base64Helper class is provided, and Base64 string and file conversion operations are provided.
/// <Summary> /// Base64 encoded string and file conversion help tears /// </summary> public class Base64Helper {// <summary> // Base64 encoded string convert file /// </summary> /// <param name = "base64"> Base64 encoded string </param> /// <param name = "filename"> complete file path </param> public static void ConvertBase64ToFile (string base64, string filename) {// base64 = "/9j/4 samples/2 samples/2 samples/wAARCABkAGQDASIAAhEBAxEB/8 samples/samples + Tl5ufo6erx8vP09fb3 + Pn6/8 samples/samples + Pn6/samples/tRfE/ wDZx + release/sh + Opf2p/release/hnSNHt9Pt/release/GPx2 + IM/jr4q61e + release/release + zZ8Ofh/4F0/release + release/YL + LHgf4zft/8AgvwL8c/release + examples/examples + KPFt1qHiU + examples/Sl/4 RIHkx/pXpqsRZnA2uj + examples + AYPJ/HA/GvUbzQNFglis2m + example/wC9JJ2 + Tb + example + HNHhJx /FHpttGw/BlI/CubPhnI/samples + Eb29ENB8O + samples/samples + Hjn/V/samples/SrkGhL/c/samples + samples/FH9mX4m + GrXWJYLvw /examples/8ABQK3 + GtvqPw0 + examples/examples + samples + FNH/6i + 6sH4Yav4V + samples/hH9mz/mBbf/J/NfP5/Ubjg9b/AL + n + samples/HH9TyJ /release + nVU9DQ8 + l8PYP + r/sq0vl/nHl4 + release/release + release/wDiB + w/D8T/AI72fjv4Z + Dbu/release/ADcMPpu + 8P6v438K2 + release /g14vk8A + examples/Ur6DT9Ut/examples + xonpY + Uv2Y/2j/i3 + examples/Cv4peHPiTpU/examples + Jf + Cklz4/uviVb/Hr4c/examples + examples/utN2 + examples + HNPXRATZCxi + yFgcmLYNmc/wCzip5NNU/w/pXFfsqftK + Af2nfBN/r/release + release/i7Q/CB0 + DVJi91qt + release/release /users + users/users + E + v6Bq91e + I/users/iLwv8RLfxf/users + I/Others/users/C + users/aA8G6n + zP4pj0rV9du/EnhD/users release + I7P4V + MdPuLDU1lOt + release + 2B4Q8SXHjbQ5LHxlZ6M2m + release + cP + CXaeO/Gn7X2lx + release/wTs/4J + release/release + r + release/ niZnYxoA9PUPD/h268Vavq + kNo11aSSm3 + encrypt/decrypt/h/encrypt + encrypt/decrypt/Zi8O6f + encrypt/decrypt + h6Oy/wB4Ux4gelT/combine + encrypt/decrypt/ ztXnZX + 7I + Wkii/cores + cores/smIx + B + 8Z8qsjrsVUCs + JVfaF + cores/cores + H/cores + MNHiuf8ADZk1/cores + cores/Gix + export/4I + GXiPxppMMMl1o + export/RYtO0MavJe3V1eauEvL + export + LD2 + export/goZ4AHr4Xn/APRWoV7 + export //"; 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> /// convert the file to a Base64 encoded string /// </summary> /// <param name = "fileName"> complete file path </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 ;}}