views:

3868

answers:

5

Hi,

I need to convert uploaded binary files to base64 string format on the fly. I'm using ASP, Vbscript. Using Midori's component for base64 conversion. For small size files (<20K) the performance is okay. But when it exceeds 75 or 100K, its totally lost. Is there any efficient way to convert big binary files (2MB) to base64 string format?

Thanks in advance, Kenney

+1  A: 

you should use the .NET methods Convert.ToBase64String and Convert.FromBase64String.

Use the Convert.FromBase64String( ) method. This will give you the binary data back (as a byte array).

To convert binary data to a Base64 string see conversion functions from binary data to a string in vbscript

from http://www.motobit.com/tips/detpg_Base64Encode/

Function Base64EncodeBinary(inData)
  Base64EncodeBinary = Base64Encode(BinaryToString(inData))
End Function

Function Base64Encode(inData)
  'rfc1521
  '2001 Antonin Foller, Motobit Software, http://Motobit.cz
  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim cOut, sOut, I

  'For each group of 3 bytes
  For I = 1 To Len(inData) Step 3
    Dim nGroup, pOut, sGroup

    'Create one long from this 3 bytes.
    nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
      &H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))

    'Oct splits the long To 8 groups with 3 bits
    nGroup = Oct(nGroup)

    'Add leading zeros
    nGroup = String(8 - Len(nGroup), "0") & nGroup

    'Convert To base64
    pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)

    'Add the part To OutPut string
    sOut = sOut + pOut

    'Add a new line For Each 76 chars In dest (76*3/4 = 57)
    'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
  Next
  Select Case Len(inData) Mod 3
    Case 1: '8 bit final
      sOut = Left(sOut, Len(sOut) - 2) + "=="
    Case 2: '16 bit final
      sOut = Left(sOut, Len(sOut) - 1) + "="
  End Select
  Base64Encode = sOut
End Function

Function MyASC(OneChar)
  If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function
joe
Krish,The same code has been implemented in Midori's component and i'm using that.
Krish, could you please let me know how to accomplish this in ASP - vbscript environment. My application doesn't support .net :(
try this . it might be helpful
joe
Krish,SimpleBinaryToString() - Will be working fine for ASCII files, but not for binary files. I overcome this issue by converting the binary to an array of Ascii integer codes and get converted to base64 by Midori's component. The problem is, its consuming more time for this conversion and i cant handle big files. :(
I guess better you can use some softwares to transfer the data to your porgram. I will put new code soon
joe
when its large file .. it will take more time ..
joe
A: 

Use MSXML to do the encoding for you. Here is function encapsulating the procedure:-

 Function ToBase64(rabyt)

     Dim xml: Set xml = CreateObject("MSXML2.DOMDocument.3.0")
     xml.LoadXml "<root />"
     xml.documentElement.dataType = "bin.base64"
     xml.documentElement.nodeTypedValue = rabyt

     ToBase64 = xml.documentElement.Text

 End Function

Note this will include linebreaks in the base64 encoding but most base64 decoders are tolerant of linebreaks. If not you could simpy use Replace(base64, vbLF, "") to remove them, this will still be quicker than a pure VBScript solution.

Edit Example usage:-

Dim sBase64: sBase64 = ToBase64(Request.BinaryRead(Request.TotalBytes))
AnthonyWJones
Hi,Thanks for the reply. I'm converting a binary file to base64. Binary file is read from Request object. What is type of the input (rabyt)? TIA
@Kenny: rabyt is a byte array.
AnthonyWJones
Hi Anthony W Jones,My request object has multiple binary files uploaded. I use BinaryRead() to fetch the wholde request object. And from the byteArray i use MidB() and get the individual files. MidB() returns the files in string vartype. How do I get each file as byteArray?
If you have multiple files in multipart post then things get really tricky with classic ASP especially if you can't include any custom componentry on the server.
AnthonyWJones
A: 

I use next code for c#:

    public static string ImageToBase64(Image image, ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            // Convert Image to byte[]
            image.Save(ms, format);
            byte[] imageBytes = ms.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }

    public static Image Base64ToImage(string base64String)
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        MemoryStream ms = new MemoryStream(imageBytes, 0,
          imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        Image image = Image.FromStream(ms, true);
        return image;
    }

for vbscript see http://www.freevbcode.com/ShowCode.asp?ID=5248 maybe help you.

pho3nix
How is the C# code useful in this context?
AnthonyWJones
A: 

There is a good discussion of this in base64-encode-string-in-vbscript.

In addition, I have found this site useful for trying to eek speed out of vb code. There are several variants of base 64 there for vb6 that are quite fast.

pflunk
+1  A: 

I have solved this issue by implementing a .net component for converting to base64 string. The hard part is the binary data sent to the .net COM from ASP is received as a string. Convert.ToBase64() accepts only byte[]. So I tried converting string to byte[].

But the encoding available in .net (Unicode, ASCII, UTF) doesn't works fine. There are data loss, while these encodings are used. Finally I get it done by using StringReader object. Read char by char(16 bit) and converted them to (8 bit) byte[] array.

And the performance is best.

Regards, Siva.

Thanks for the solution Siva