Interchange Upload/Download

Upload files in a specified directory to the Session Network/Mailbox and Download all new Parcels in the Session Network/Mailbox to a specified directory.

Visual Basic

'ECGridOSMailboxIO by Loren Data Corp.
'This public domain code demonstrates how to use ECGridOS to upload and download
'files from ECGrid(r)
'See ECGridOS documentation at
'Request ECGridOS credentials at
'The following ECGridOS calls are used:
' Login()
' Logout()
' ParcelUpload()
' ParcelUploadGZip()
' ParcelDownload()
' ParcelDownloadGZip()
' ParcelDownloadConfirm()
' ParcelInBox()
'The syntax of this function is:
'ECGridOSMailboxIO <UserID> <Password> <directory> <-upload|-download> [-gzip]
' UserID - must be previously registered on ECGridOS
' Password - must be previously registered on ECGridOS
' directory - the local directory - if upload, all files in the directory
'             will be sent
' -upload|-download - direction of file transfer
' -gzip - if present, then GZIP compression is used.
'ECGrid is a registered service mark of Loren Data Corp.
'ECGridOS is a service mark of Loren Data Corp.
Imports System.Xml
Imports System.IO
   'ECGRIDOS: Set up an Object for the Web Service;
   ' this requires a Web Reference to

   Public ecgridos As New net.ecgridos.ECGridOSAPIv2
   'ECGRIDOS: Have a place to store the SessionID
   Public SessionID As String
   Sub Main()
      If My.Application.CommandLineArgs.Count < 4 Then
         Dim UserID As String = My.Application.CommandLineArgs(0)
         Dim Password As String = My.Application.CommandLineArgs(1)
         Dim FileDir As String = My.Application.CommandLineArgs(2)
         Dim Upload As Boolean = My.Application.CommandLineArgs(3).ToLower = _
         Dim GZip As Boolean = False
         If My.Application.CommandLineArgs.Count = 5 Then
            GZip = My.Application.CommandLineArgs(4).ToLower = "-gzip"
         End If

My.Application.Info.ProductName & ": " & _
          FormatDateTime(Now, DateFormat.ShortDate))
            'ECGRIDOS: Always Login first
            SessionID = ecgridos.Login(UserID, Password)
            If Upload Then
               ECGridOSUpload(FileDir, GZip)
               ECGridOSDownload(FileDir, GZip)
            End If
         Catch ex As SoapException
            'There is good data in the InnerXML
            'which can be parsed and used to processes specific exceptions
            Dim doc As New XmlDocument
            Dim Node As XmlNode
            Node = doc.DocumentElement.SelectSingleNode(
            If Not Node Is Nothing Then
               Console.WriteLine([String].Format( _
"SOAP Exception: ({0}) {1}", _
"ErrorCode").InnerText, _
" Error Item: {0}", _
" Msg: {0}", _
               Console.WriteLine("ERROR: " & ex.Message.ToString)
            End If
         Catch ex As Exception
"ERROR: " & ex.ToString)
            'ECGRIDOS: Make sure to Logout when done.
         End Try
      End If
      ecgridos = Nothing
   End Sub
   Private Sub Syntax()
"ECGridOSMailboxIO <UserID> <Password>" & _
       " <upload|download> <localdirectory>"
   End Sub
   Private Sub ECGridOSUpload(ByVal FileDir As String, _
                              ByVal GZip As Boolean)
      Dim fs As IO.FileStream
      Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
      Dim buffer() As Byte
      Dim bytes As Long
      Dim ParcelID As Integer
      'Let's send all files in the specified directory
      files =
My.Computer.FileSystem.GetFiles(FileDir, _
       FileIO.SearchOption.SearchTopLevelOnly, _
      For Each FileName As String In files
"Uploading: " & FileName & "...")
         'Load the entire file into a string buffer
         bytes = FileLen(FileName)
         fs =
New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)
         ReDim buffer(bytes - 1)
         fs.Read(buffer, 0, bytes)
         If GZip Then
           Dim ms As New MemoryStream
           'Make sure that LeaveOpen:=true so that the underlying stream (ms)
           'maintains control.
           Dim gz As New GZipStream(ms, CompressionMode.Compress, True)
           gz.Write(buffer, 0, buffer.Length)
           'Be sure to close the GZipStream here or the buffer will not
           'flush properly.
           ReDim buffer(ms.Length - 1)
           ms.Position = 0
           ms.Read(buffer, 0, ms.Length)
           'ECGRIDOS: ParcelUpload() posts a file to ECGrid
           ' The ParcelID can be used as a handle later to get more information
           ' about the specific files as it transits the system.
           ParcelID = ecgridos.ParcelUploadGZip(SessionID, _
            Path.GetFileName(FileName), bytes, buffer)
           'ECGRIDOS: ParcelUpload() posts a file to ECGrid
           ' The ParcelID can be used as a handle later to get more information
           ' about the specific files as it transits the system.
           ParcelID = ecgridos.ParcelUpload(SessionID, _
            Path.GetFileName(FileName), bytes, buffer)
        End If
         'Delete the file after a successful upload.
         'Note that the Try/Catch from the calling routine will handle any errors
         ' and not delete the file if it doesn't upload
   End Sub
   Private Sub ECGridOSDownload(ByVal FileDir As String, _
                                ByVal GZip As Boolean)
      Dim parcels() As net.ecgridos.ParcelIDInfo
      Dim ParcelID As Integer
      'The FileInfo object holds all the info and payload of the downloaded file
      Dim FileInfo As net.ecgridos.FileInfo
      Dim fs As IO.FileStream
      Dim buffer() As Byte
      If Left(FileDir, 1) <> "\" Then FileDir = FileDir & "\"
      'ECGRIDOS: ParcelInBox() is used to lists all pending files in the current
' Mailbox("In Box"). A collection/array of ParcelIDInfo objects is returned.
parcels = ecgridos.ParcelInBox(SessionID)
      For Each parcel In parcels
"Downloading: " & parcel.FileName & "...")
         ParcelID = parcel.ParcelID
         If GZip Then
            'ECGRIDOS: ParcelDownloadGZip() is used to download the
            ' File info & content; 
it returns the FileInfo object with
            ' the Content GZip compressed.
            FileInfo = ecgridos.ParcelDownloadGZip(SessionID, ParcelID)
            Dim ms As New MemoryStream()
            Dim os As New MemoryStream()
            Dim buf() As Byte
            Dim b As Integer
            ms.Write(FileInfo.Content, 0, FileInfo.Content.Length)
            ms.Position = 0
            Dim gz As New GZipStream(ms, CompressionMode.Decompress, True)
            'One would think you could do this in one shot, but GZipStream
            'has it's own ideas. You won't even get a consistent 4096 bytes
            'ever time you loop through.
            buf = New Byte(4095) {}
               b = gz.Read(buf, 0, buf.Length)
               os.Write(buf, 0, b)
               If os.Length = FileInfo.Bytes Then Exit Do
               If b < 1 Then Exit Do
            ReDim FileInfo.Content(FileInfo.Bytes - 1)
            os.Position = 0
            os.Read(FileInfo.Content, 0, os.Length)
            'ECGRIDOS: ParcelDownload() is used to download the File
            ' info & content; 
it returns the FileInfo object
            FileInfo = ecgridos.ParcelDownload(SessionID, ParcelID)
         End If

         'Save the payload to a file as the same name as in the FileInfo object
         ' in the commandline specified directory.
         buffer = FileInfo.Content
         fs =
New IO.FileStream(FileDir & FileInfo.FileName, _
          IO.FileMode.Create, _
         fs.Write(buffer, 0, FileInfo.Bytes)
         'ECGRIDOS: ParcelDownloadConfirm is used to tell ECGrid to mark the file
         ' as downloaded and remove it from the InBox. A copy of the file remains
         ' in the Archive.
         ecgridos.ParcelDownloadConfirm(SessionID, ParcelID)
   End Sub

See Also


© 2008-2011, Loren Data Corp.
All rights reserved.
ECGrid is a registered service mark of Loren Data Corp.
ECGridOS is a service mark of Loren Data Corp.