在 UFT 中使用 vbs 将 zip 文件转换为 base64

convert a zip file to base64 using vbs in UFT

我需要将 zip 文件从我的本地计算机转换为 base64。

  1. Get/Read 来自 excel sheet 行的路径名

  2. 将路径中的文件(zip文件)转换为base 64字符串

  3. 将 base 64 值复制到 excel sheet 中的下一列。

尝试了一些但没有用。

当前代码:

Dim inByteArray, base64Encoded

inByteArray = readBytes("F:path/file.zip")
base64Encoded = encodeBase64(inByteArray)

Private Function readBytes(file)
    Dim inStream
    ' ADODB stream object used
    Set inStream = CreateObject("ADODB.Stream")
    ' open with no arguments makes the stream an empty container 
    inStream.Open
    inStream.Type = TypeBinary
    inStream.LoadFromFile(file)
    readBytes = inStream.Read()
End Function

Private Function encodeBase64(bytes)
    Dim DM, EL
    Set DM = CreateObject("Microsoft.XMLDOM")
    ' Create temporary node with Base64 data type
    Set EL = DM.CreateElement("tmp")
    EL.DataType = "bin.base64"
    ' Set bytes, get encoded String
    EL.NodeTypedValue = bytes
    encodeBase64 = EL.Text
End Function

inStream.type = TypeBinary 行中的错误 1:

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

readBytes = inStream.Read() 行中的错误 2:

Operation is not allowed in this context.

EL.NodeTypedValue = bytes 行中的错误 3:

Type mismatch

您似乎从 here 获得了代码,但没有包含

Const TypeBinary = 1

添加这个可以避免 "Arguments are of the wrong type ..." 错误。

也许细心复制也能解决您的其他问题。

谢谢你:)

进一步 excel sheet 读写我使用了下面的代码,这有助于实现我的目标。谢谢

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("F:\path")

Set ws = objWorkbook.Sheets("Sheet1")
Set ws2 = objWorkbook.Sheets("Sheet2")
rowcount = ws.usedrange.rows.count

for j = 1 to rowcount

                fieldvalue = ws.cells(j,1)


               inByteArray = readBytes(fieldvalue)
               base64Encoded = encodeBase64(inByteArray)
               ws2.cells(j,1) = base64Encoded   
next