C# Fax - Send Large Files Using UploadFileChunk

Download the C# fax API zip file.

The UploadFileChunk method can be used to upload a file of 500KB and upwards (in a configurable number of chunks).

To use the sample, download the C# project file and extract it to a local folder.

The project files demonstrate how to use the inbound C# fax Web service's UploadFileChunk and SendfaxEx_2 methods.

The following snippet, which can be found in the downloaded project files (class1.cs), reads the file data, uploads the file using the UploadFileChunk method, and then sends the file using the SendfaxEx_2 method:


            using System;
using System.IO;

namespace UploadFileChunk
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string username = "";
            string password = "";
            string faxNumbers = "00442071499926"; // to send to multiple numbers, separate by [;] without the brackets
            string contacts = ""; // to send to multiple contacts, separate by [;] without the brackets
            string pathFileToRead = "C:\\Docs\\Interfax\\iftestwslex2.pdf"; // path to the file to be uploaded
            // get the file extension
            string fileType = System.IO.Path.GetExtension(pathFileToRead).TrimStart('.');
            // initializing for later. must be a string in the form of [filesize];[filesize2]/sessionID=[sessionID]
            string fileSize = "";
            // must initialize session id for it to be referenced later
            string sessionID = "";
            // recommended between 50,000 to 200,000 bytes
            int chunkSize = 50000;
            // create file stream to read the file into a byte array
            FileStream createStreamFaxImage = new FileStream(pathFileToRead, FileMode.Open, FileAccess.Read);
            interfax.InterFax interfaxWebServiceOutbound = new interfax.InterFax();
            // get a session id
            interfaxWebServiceOutbound.StartFileUpload(username, password, ref sessionID);
            // check to see if this is the last chunk to be uploaded
            bool isLastChunk = false;
            // counter to determine uploaded bytes
            int totalBytesRead = 0;
            // checking if upload encounters errors
            int verboseUploading;
            while (!isLastChunk) // until all file data is read
            {
                // create a new byte array of the size of the chunkSize specified
                byte[] currentChunk = new byte[chunkSize];
                // size in bytes of number of bytes read from the current chunk
                int bytesReadFromCurrentChunk = createStreamFaxImage.Read(currentChunk, 0, currentChunk.Length);
                totalBytesRead += bytesReadFromCurrentChunk;
                if (bytesReadFromCurrentChunk == currentChunk.Length) // if this is a full buffer
                {
                    // write it normally
                    verboseUploading = interfaxWebServiceOutbound.UploadFileChunk(sessionID, currentChunk, isLastChunk);
                    if (verboseUploading >= 0) // chunk uploading was ok 
                    {
                        Console.WriteLine(string.Format("Chunk of {0} bytes uploaded successfully.", verboseUploading));
                    }
                    else // chunk uploading encountered an error
                    {
                        Console.WriteLine(string.Format("Failed to upload chunk with error code {0}.", verboseUploading));
                    }
                }
                else // if a partial buffer was read (happens on last chunk)
                {
                    // create a new byte array of the size of the last chunk
                    byte[] lastCurrentChunk = new byte[bytesReadFromCurrentChunk];
                    for (int i = 0; i < bytesReadFromCurrentChunk; i++)
                    {
                        lastCurrentChunk[i] = currentChunk[i];
                    }
                    // write the last chunk
                    isLastChunk = true;
                    verboseUploading = interfaxWebServiceOutbound.UploadFileChunk(sessionID, lastCurrentChunk, isLastChunk);
                    if (verboseUploading >= 0) // chunk uploading was ok 
                    {
                        Console.WriteLine(string.Format("Chunk of {0} bytes uploaded successfully.", verboseUploading));
                    }
                    else // chunk uploading encountered an error
                    {
                        Console.WriteLine(string.Format("Failed to upload chunk with error code {0}.", verboseUploading));
                    }
                }
            }
            fileSize = string.Format("{0}/sessionID={1}", totalBytesRead.ToString(), sessionID);
            // set postpone time. for no postpone time, use a date in the past
            DateTime postponeTime = DateTime.Now.AddHours(0);
            int retriesToPerform = 3;
            string csid = "InterFAX";
            // for page header formatting, see options
            string pageHeader = "To: {To} From: {From} Pages: {TotalPages}";
            string subject = "Test WS Fax";
            string replyAddress = "";
            string pageSize = "A4";
            string pageOrientation = "Portrait";
            // use high resolution for maintaining original document scale (increases transmission duration)
            bool isHighResolution = false;
            // use fine rendering for graphical elements
            bool isFineRendering = false;
            // end of parameters
            long resultStatus = interfaxWebServiceOutbound.SendfaxEx_2(username, password, faxNumbers, contacts, null, fileType, fileSize, postponeTime, retriesToPerform, csid,
            pageHeader, "", subject, replyAddress, pageSize, pageOrientation, isHighResolution, isFineRendering);
            if (resultStatus > 0) // if the result is positive, it's the TransactionID created
            {
                Console.WriteLine(string.Format("Created Fax with TransactionID {0}.", resultStatus));
            }
            else // if the result is negative, it's an error code
            {
                Console.WriteLine(string.Format("Failed with Error Code {0}.", resultStatus));
            }
            Console.ReadLine(); // use this to keep console open (waiting for keypress) after it is finished
        }
    }
}

Contact us today

Talk to a member of our team about the benefits InterFAX can bring to your organization's communications processes.

Contact us today