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

时间:2016-12-15 10:14:03

标签: vbscript zip base64

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

  1. 从Excel工作表行获取/读取路径名

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

  3. 将基础64值复制到Excel工作表中的下一列。

  4. 尝试了一些但没有工作。

    当前代码:

    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:

      

    参数类型错误,超出可接受的范围,或彼此冲突。

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

      

    在此上下文中不允许操作。

    EL.NodeTypedValue = bytes中的错误3:

      

    类型不匹配

2 个答案:

答案 0 :(得分:2)

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

Const TypeBinary = 1

添加此项将避免"参数类型错误..."错误。

也许小心副本也可以解决您的其他问题。

答案 1 :(得分:0)

谢谢你们:)

此外,对于excel表读取和写入,我使用了以下代码,这有助于实现我的目标。谢谢

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