2015-07-11 76 views
0

我想将字节[]从excel vba传递到Web服务。如何将字节[]从Excel vba传递到Web服务

下面是将文件转换为byte []的代码。

Dim bytFile() As Byte 
bytFile = GetFileBytes("C:\test.doc") 

以下代码用于调用webservice。 bytFile是我的参数

Dim XMLHttp As Object: Set XMLHttp = CreateObject("Microsoft.XMLHTTP") 
XMLHttp.Open "POST", Service + "/PassExcelData", False 
XMLHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
XMLHttp.send "filebyte=" & bytFile 

但我无法将它传递给webservice。我在最后一行发生错误。所以我只是想这可能吗?如果没有,我可以实现这个目标的方式?

我也曾尝试下面的代码

Function FileToStr(ByVal strFile As String) As String 
Dim hFile As Long 
hFile = FreeFile 
Open strFile For Input As #hFile 
FileToStr = Input$(LOF(hFile), hFile) 
Close #hFile 
End Function 

,并把它称为是:

XMLHttp.send "filebyte=" & FileToStr(file path). 

,但它返回下面的错误:

enter image description here

回答

0

您不能追加一个带有&运算符的字节数组。获得使用 使用此功能的文件的文本

Function FileToStr(ByVal strFile As String) As String 
    Dim hFile As Long 
    hFile = FreeFile 
    Open strFile For Input As #hFile 
    FileToStr = Input$(LOF(hFile), hFile) 
    Close #hFile 
End Function 

,然后做 XMLHttp.send“filebyte =” & FILETOSTR(“路径文件”)

您可以注释掉GetFileBytes线

+0

让我试试看,我会让你知道结果 – User5590

+0

请看我编辑的问题 – User5590

相关问题