VB.NET: Upload A File For Future Use Using UploadFile()
Instructions
- Create a new Console Application project and add a web reference.
- Copy the entire code below, delete anything written in your application and paste the code there.
- Replace the values of the Const lines with your details:
- Replace MyUsername and MyPassword with your PostalMethods user details.
- Replace MyFileName with the name you would like to give the file. This name is used for future references to this file. Format:
<file name>.<file extension>. - Replace MyPathToFile with a full path to your file.
- Start your application. If the response is -3000, you will see the status report. If the response is any other number, check the Web Service Status Codes section.
Imports System.IO Module module1 Sub Main() Const MyUsername As String = "MyUsername" Const MyPassword As String = "MyPassword" Const MyFileName As String = "PurchaseInvoice.mht" Const MyPathToFile As String = "C:\PurchaseInvoice.mht" ' You may download this file from http://www.postalmethods.com/files/templates/PurchaseInvoice.mht ' Set account permissions for this file: Const MyPermissions As com.postalmethods.api.Permissions = com.postalmethods.api.Permissions.Account ' Set Overwrite mode: Const FileUploadOverwrite As Boolean = False Dim objPM As New com.postalmethods.api.PostalWS() Dim lngResult As Long ' Read file to upload and convert to binary. In this case, the file is C:\PurchaseInvoice.mht ' You may download this file from http://www.postalmethods.com/files/templates/PurchaseInvoice.mht Dim MyFile As Byte() = IO.File.ReadAllBytes(MyPathToFile) ' Invoke the UploadFile method lngResult = objPM.UploadFile( _ MyUsername, _ MyPassword, _ MyFileName, _ MyFile, _ MyPermissions, _ FileUploadOverwrite) ' ' Check response status ' If lngResult = -3000 Then ' ' If the result code = -3000, the file was uploaded successfully and it is ready for use. ' Console.WriteLine("File uploaded successfully!") Else ' ' A negative value means an error occurred. ' See the PostalMethods Status Codes: http://www.postalmethods.com/statuscodes#webservice. ' Console.WriteLine("An error occurred:" & lngResult) End If ' In debug mode, the following lines prevent your console application from closing automatically upon termination Console.WriteLine("Hit ENTER to terminate application") Console.ReadLine() End Sub End Module

