0
votes

I can get file from template which passing templateid and documentid, when retrun from api , i will get as base64string format, and when i try to convert as byte,

I get an error "Additional information: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."

Here the code which i used to download file from template

    Dim rest As New Chilkat.Rest
    Dim success As Boolean
    Dim bTls As Boolean = True
    Dim port As Integer = 443
    Dim bAutoReconnect As Boolean = True
    success = rest.Connect("demo.docusign.net", port, bTls, bAutoReconnect)
    If (success <> True) Then
        Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
        Debug.WriteLine(rest.LastErrorText)
        Exit Sub
    End If


    rest.AddHeader("X-DocuSign-Authentication", "{ ""Username"":    ""[email protected]"",  ""Password"":""DocuSign_password"",  ""IntegratorKey"":""DocuSign_Integrator_Key"" }")

    Dim sbResponseBody As New Chilkat.StringBuilder
    
    success = rest.FullRequestNoBodySb("GET", "/restapi/v2.1/accounts/xxxx/templates/xxxx-xxxx-xxxx-xxxx-xxxx/documents/x", sbResponseBody)
    
    If (success <> True) Then
        Debug.WriteLine(rest.LastErrorText)
        Exit Sub
    End If

    Dim respStatusCode As Integer = rest.ResponseStatusCode
    If (respStatusCode >= 400) Then
        Debug.WriteLine("Response Status Code = " & respStatusCode)
        Debug.WriteLine("Response Header:")
        Debug.WriteLine(rest.ResponseHeader)
        Debug.WriteLine("Response Body:")
        Debug.WriteLine(sbResponseBody.GetAsString())
        Exit Sub
    End If
    Dim jsonResponse As New Chilkat.JsonObject
    jsonResponse.LoadSb(sbResponseBody)
    Dim pdfBytes As Byte() = Convert.FromBase64String(sbResponseBody.ToString)

Regards, Aravind

1

1 Answers

0
votes

i changed my code from chilkat dll to Docusing dll 3.0.0 , using docusing dll i can download file from envelope, here i past my code , it will useful to some other developer.I am used .net framework code download file from file stream.

Private Function DoWnload(ByVal accessToken As String, ByVal basePath As String, ByVal accountId As String, ByVal envelopeId As String, ByVal documents As List(Of EnvelopeDocItem), ByVal docSelect As String) As String
    Dim config = New Configuration(New ApiClient(basePath))
    config.AddDefaultHeader("Authorization", "Bearer " & accessToken)
    Dim envelopesApi As EnvelopesApi = New EnvelopesApi(config)
    Dim results As System.IO.Stream = envelopesApi.GetDocument(accountId, envelopeId, docSelect)
    Dim docItem As EnvelopeDocItem = documents.FirstOrDefault(Function(d) docSelect.Equals(d.DocumentId))
    Dim docName As String = docItem.Name
    Dim hasPDFsuffix As Boolean = docName.ToUpper().EndsWith(".PDF")
    Dim pdfFile As Boolean = hasPDFsuffix
    Dim docType As String = docItem.Type

    If ("content".Equals(docType) OrElse "summary".Equals(docType)) AndAlso Not hasPDFsuffix Then
        docName += ".pdf"
        pdfFile = True
    End If

    If "zip".Equals(docType) Then
        docName += ".zip"
    End If

    Dim mimetype As String

    If pdfFile Then
        mimetype = "application/pdf"
    ElseIf "zip".Equals(docType) Then
        mimetype = "application/zip"
    Else
        mimetype = "application/octet-stream"
    End If
    Dim bytesRead As Integer
    Dim buffer(4096) As Byte

    Using outFile As New System.IO.FileStream("C:\File.pdf", IO.FileMode.Create, IO.FileAccess.Write)
        Do
            bytesRead = results.Read(buffer, 0, buffer.Length)
            If bytesRead > 0 Then
                outFile.Write(buffer, 0, bytesRead)
            End If
        Loop While bytesRead > 0
    End Using
    Return ""

End Function

Dim envelopeDocItems As List(Of EnvelopeDocItem) = New List(Of EnvelopeDocItem) From {
New EnvelopeDocItem With {
    .Name = "Combined",
    .Type = "content",
    .DocumentId = "combined"
},
New EnvelopeDocItem With {
    .Name = "Zip archive",
    .Type = "zip",
    .DocumentId = "archive"
}

Thanks to Inbar Gazit...

Thanks and Regards, Aravind