Can I use VBScript to base64 encode a gif?

孤人 提交于 2020-01-25 07:38:06

问题


What I'm trying to do is encode a gif file, to include in an XML document. This is what I have now, but it doesn't seem to work.

Function gifToBase64(strGifFilename)
 On Error Resume Next
 Dim strBase64
 Set inputStream = WScript.CreateObject("ADODB.Stream")
 inputStream.LoadFromFile strGifFilename
 strBase64 = inputStream.Text
 Set inputStream = Nothing
 gifToBase64 = strBase64
End Function

回答1:


I recently wrote a post about this very subject for implementations in JScript and VBScript. Here is the solution I have for VBScript:

Public Function convertImageToBase64(filePath)
  Dim inputStream
  Set inputStream = CreateObject("ADODB.Stream")
  inputStream.Open
  inputStream.Type = 1  ' adTypeBinary
  inputStream.LoadFromFile filePath
  Dim bytes: bytes = inputStream.Read
  Dim dom: Set dom = CreateObject("Microsoft.XMLDOM")
  Dim elem: Set elem = dom.createElement("tmp")
  elem.dataType = "bin.base64"
  elem.nodeTypedValue = bytes
  convertImageToBase64 = "data:image/png;base64," & Replace(elem.text, vbLf, "")
End Function



回答2:


In your comment to Tomalak you state you don't want to use external dlls but in your attempted example you try to use ADODB. I suspect therefore what you mean is you don't want to install dlls that aren't natively present on a vanilia windows platform.

If that is so then MSXML may be your answer:-

Function Base64Encode(rabyt)

    Dim dom: Set dom = CreateObject("MSXML2.DOMDocument.3.0")
    Dim elem: Set elem = dom.appendChild(dom.createElement("root"))
    elem.dataType = "bin.base64"
    elem.nodeTypedValue = rabyt

    Base64Encode = elem.Text

End Function



回答3:


Take a look here: Base64 Encode & Decode Files with VBScript. This example relies on the free XBase64 component and merely provides a wrapper for file handling.

You can also go for a pure VBScript implementation, but here you have to care for the file handling yourself. Should not be too difficult, but encoding performance will be not as good. For a few small image files it will be enough, though.

Google will turn up more.



来源:https://stackoverflow.com/questions/188793/can-i-use-vbscript-to-base64-encode-a-gif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!