Question

I am having some sort of problem with encoding in my ASP.NET HTTPHandler, which uploads a file. The file content is passed in a hidden form variable from a ColdFusion web page which is using something called "ToBase64".

In ColdFusion, the code used to place the file content into a form is as follows:

<cffile action="readBinary" file="#FileName#" variable="objBinaryData">
    <cfset b64file = #toBase64(objBinaryData)#>
<form name="sendToHandler" 
           action="http://myserver/mysite/UploadHandler.ashx" method="post">
   <cfoutput>
       <input type="hidden" name="objBinaryData" value="#b64file#" />

When my UploadHandler.ashx is posted, I am getting a string out of the form as follows:

            string fileContent = context.Request.Form["objBinaryData"];

Next, I am converting the string to a byte array as follows:

            byte[] binData = StringToByteArray(fileContent, EncodingType.ASCII);

Here is the function I'm using to convert the string:

        public static byte[] StringToByteArray(string str, EncodingType encodingType)
    {
        System.Text.Encoding encoding = null;
        switch (encodingType)
        {
            case EncodingType.ASCII:
                encoding = new System.Text.ASCIIEncoding();
                break;
            case EncodingType.Unicode:
                encoding = new System.Text.UnicodeEncoding();
                break;
            case EncodingType.UTF7:
                encoding = new System.Text.UTF7Encoding();
                break;
            case EncodingType.UTF8:
                encoding = new System.Text.UTF8Encoding();
                break;
        }
        return encoding.GetBytes(str);
    }
public enum EncodingType
    {
        ASCII,
        Unicode,
        UTF7,
        UTF8
    }

It's obvious to me that calling the above function with EncodingType.ASCII is wrong but I am very confused about what would be correct? What is the proper "match" between "Base64" sent from ColdFusion and the way the string should be encoded in .Net?

Please note that all the code "works" but the subsequent retrieval of a file shows it to be scrambled and I'm pretty sure I have the wrong encoding here.

EDIT-update:

I added the enum code previously omitted. I've tried all of these Encoding Types; they all result in "garbage". That is: I have tried each of these variations:

byte[] binData = StringToByteArray(fileContent, EncodingType.ASCII);
byte[] binData = StringToByteArray(fileContent, EncodingType.Unicode);
byte[] binData = StringToByteArray(fileContent, EncodingType.UTF7);
byte[] binData = StringToByteArray(fileContent, EncodingType.UTF8);

None of these work properly. As I read your suggested function, it should be Unicode. Note that I want to return a byte array not a converted string. Still very confused.

ANSWER:

I simply eliminated the enum and the function I wrote called StringToByteArray. Instead I coded the following:

byte[] binData = Convert.FromBase64String(fileContent); 
Was it helpful?

Solution

Look at the Convert.FromBase64String() function

OTHER TIPS

Base64 is an encoding scheme that enables you to represent binary data as a series of ASCII characters so that it can be included in text files and e-mail messages in which raw binary data is unacceptable. The below examples show encoding and decoding of unicode strings. Let me know if this is what you wanted,if not I can refind this further for you.

//Encoding
 public static string StringToBase64 (string src) {

    // Get's byte representation unicode string
    byte[] b = Encoding.Unicode.GetBytes(src);

    // Returns Base64-encoded string
    return Convert.ToBase64String(b);

}
//Decoding
public static string Base64ToString (string src) {

    // Decodes Base64-encoded string to a byte array
    byte[] b = Convert.FromBase64String(src);

    // Returns decoded Unicode string
    return Encoding.Unicode.GetString(b);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top