Converting binary file to Base64 string

后端 未结 6 1552
小鲜肉
小鲜肉 2021-01-13 06:41

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 (

相关标签:
6条回答
  • 2021-01-13 07:18

    This is what worked for me

    Function Base64DataURI(url)
    
        'Create an Http object, use any of the four objects
        Dim Http
        Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
    
        'Send request To URL
        Http.Open "GET", url, False
        Http.Send
        'Get response data As a string and encode as base64
        Base64DataURI = Encrypt(Http.ResponseText)
    End Function
    

    In my case the URL is a script that generates a barcode on the fly and needed to encode to include that in emails.

    Encrypt is a pretty standard function we use to encode as Base64, but the main concept we needed was to get the file via URL not file system.

    0 讨论(0)
  • 2021-01-13 07:22

    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))
    
    0 讨论(0)
  • 2021-01-13 07:30

    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
    
    0 讨论(0)
  • 2021-01-13 07:35

    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.

    0 讨论(0)
  • 2021-01-13 07:36

    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.

    0 讨论(0)
  • 2021-01-13 07:39

    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.

    0 讨论(0)
提交回复
热议问题