Jump to content

Webservice with VBA and Image


Recommended Posts

Hello,

 

i'm trying to add image to a created products Under VBA.

 

Unfortunatly i get the famous error  "<code><![CDATA[66]]></code> <message><![CDATA[unable to save this image]]></message>"

 

 

After looking over the web i did this (thank you for contributors :rolleyes: ):

 

pvPostFile("http://xxx.xxx.com/api/images/products/20", "n:\photos\imageone.jpg") 

Function pvPostFile(sUrl As String, sFileName As String, Optional ByVal bAsync As Boolean) As String
    Const STR_BOUNDARY  As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113"
    Dim nFile           As Integer
    Dim baBuffer()      As Byte
    Dim sPostData       As String
 PsWsKey = "xxxxxxxxxxxxxxxxxxxxxxx"
    '--- read file
    nFile = FreeFile
    Open sFileName For Binary Access Read As nFile
    If LOF(nFile) > 0 Then
        ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
        Get nFile, , baBuffer
        sPostData = StrConv(baBuffer, vbUnicode)
    End If
    Close nFile
    '--- prepare body
    'sPostData = "--" & STR_BOUNDARY & vbCrLf & _
    '    "Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
    '    "Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
    '    sPostData & vbCrLf & _
    '    "--" & STR_BOUNDARY & "--"
    sPostData = "--" & STR_BOUNDARY & vbCrLf & _
        "Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
        "Content-Type: image/jpeg" & vbCrLf & vbCrLf & _
        sPostData & vbCrLf & _
        "--" & STR_BOUNDARY & "--"
    
    length = Len(sPostData)
    
    '--- post
    With CreateObject("MSXML2.XMLhttp")
        .Open "POST", sUrl, bAsync
        .setRequestHeader "Accept-Charset", "ISO-8859-1,UTF-8"
        '.setRequestHeader "Host", "xxx.xxx.com"
        .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
        .setRequestHeader "Content-Length", length
        id = Base64Encode(PsWsKey & ":")
        .setRequestHeader "Authorization", "Basic " & id
        .send pvToByteArray(sPostData)
        If Not bAsync Then
            pvPostFile = .responseText
        End If
    End With


End Function

But i have always the same error while everything looks fine.

 

It's not a right error because using the traditionnal CURL method it's working

 

 

Any help is welcome and will be usefull to other members

 

Thank you

 

 

 

Link to comment
Share on other sites

  • 3 years later...

Hello

Here is the result of some hours testing 😉 lucky you are :

Public Function PostImage(sUrl As String, sFileName As String, Optional ByVal bAsync As Boolean) As String
    Const STR_BOUNDARY  As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113"
    Dim nFile           As Integer
    Dim baBuffer()      As Byte
    Dim sPostData       As String
    Dim str_Content_type As String
    
    '--- read file
    nFile = FreeFile
    Open sFileName For Binary Access Read As nFile
    If LOF(nFile) > 0 Then
        ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
        Get nFile, , baBuffer
        sPostData = StrConv(baBuffer, vbUnicode)
    End If
    Close nFile
    
    Select Case Right(sFileName, 3)
        Case "jpg"
            str_Content_type = "Content-Type: image/jpeg"
        Case "gif"
            str_Content_type = "Content-Type: image/gif"
        Case "png"
            str_Content_type = "Content-Type: image/png"
        Case Else
            str_Content_type = "Content-Type: image/jpeg"
    End Select
    
    
    '--- prepare body

    sPostData = "--" & STR_BOUNDARY & vbCrLf & _
        "Content-Disposition: form-data; name=""image""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
        str_Content_type & vbCrLf & vbCrLf & _
        sPostData & vbCrLf & _
        "--" & STR_BOUNDARY & "--"
    
    length = Len(sPostData)
    
    '--- post
    With CreateObject("MSXML2.XMLhttp")
        .Open "POST", sUrl, bAsync
        .setRequestHeader "Accept-Charset", "ISO-8859-1,UTF-8"
        .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
        .setRequestHeader "Content-Length", length
        ID = Base64Encode(pvPsWsKey & ":")
        .setRequestHeader "Authorization", "Basic " & ID
        .send pvToByteArray(sPostData)
        If Not bAsync Then
            pvPostFile = .responseText
        End If
    End With


End Function

 

Good luck, Didier

 

 

Link to comment
Share on other sites

Thank you, but it's not working for me.

I am trying to use the code for updating the order status to "finalized".

I have changed str_Content_type = "Content-Type: application/xml", but I get the error

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<message><![CDATA[Internal error. To see this error please display the PHP errors.]]></message>
</error>
</errors>
</prestashop>

 

Do you have a solution for that?

Thank you!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...