SOAP C# Example: Upload a File
The following code is a simple file upload example in C#. It uses the uploadData, beginDataUpload, isJobComplete, finishDataUpload, and getJobStatus.
Tip: The chunk size in the example shows how chunking works, it should be increased for production use.
using System; using BirstWebService; using System.IO; namespace ConsoleApplication1 { public class UploadExample { // sample constants, would normally be read from a secure location private static string USERNAME = "test@example.com"; private static string PASSWORD = "secret"; private static string FILENAME = "/var/data/testfile.txt"; private static string SPACE_ID = "1111-1111-111-1111"; private const int CHUNK_SIZE = 10 * 1024; static void Main(string[] args) { // get an instance of the web service CommandWebService cws = new CommandWebService(); // necessary to support cookie based load balancing cws.CookieContainer = new System.Net.CookieContainer(); // login to the web service string LoginToken = cws.Login(USERNAME, PASSWORD); // upload the file uploadData(cws, LoginToken, SPACE_ID, FILENAME); } private static void uploadData(CommandWebService cws, string token, string spaceId, string filename) { string source = Path.GetFileNameWithoutExtension(filename); FileStream fStream = File.OpenRead(filename); BinaryReader reader = new BinaryReader(fStream); byte[] bytes; // upload the data in chunks string dataUploadToken = cws.beginDataUpload(token, spaceId, source); do { bytes = reader.ReadBytes(CHUNK_SIZE); if (bytes.Length > 0) cws.uploadData(token, dataUploadToken, bytes.Length, bytes); } while (bytes.Length > 0); // done sending the chunks cws.finishDataUpload(token, dataUploadToken); // wait for all of the chunks to arrive, be assembled, and scanned while (!cws.isDataUploadComplete(token, dataUploadToken)) { System.Threading.Thread.Sleep(1000); } // fetch and output any warnings string[] dataUploadWarnings = cws.getDataUploadStatus(token, dataUploadToken); if (dataUploadWarnings != null && dataUploadWarnings.Length > 0) { foreach (string s in dataUploadWarnings) { Console.WriteLine(s); } } } } }