.Net Fax - Send a fax with a large file

This is a more advanced .Net code sample to show how to send a fax with a large document. This code sample is based on the .Net library for InterFAX available on Github.

This sample demonstrates how to upload a large file to InterFAX using the Document component and then sending a fax to a single recipient while referencing that previously uploaded file - all you need to do in order to change it is to provide a different fax number and a different path to the files that you would like to send. You can also change the size of the chunk that you will be uploading the file with.

  
     // read the file size
var filePath = @".\path\to\fax.pdf";
var fileInfo = new FileInfo(filePath);

// start a new document upload session
var sessionId = interfax.Outbound.Documents.CreateUploadSession(
    new UploadSessionOptions
    {
        Name = fileInfo.Name,
        Size = (int) fileInfo.Length
    }
).Result;

// open the file and start the upload
using (var fileStream = File.OpenRead("test.pdf"))
{
  // prepare a buffer for each upload
  var buffer = new byte[500];
  int len;

  // read the file into the buffer
  while ((len = fileStream.Read(buffer, 0, buffer.Length)) > 0)
  {
    var data = new byte[len];
    Array.Copy(buffer, data, len);
    // upload the chunk
    var response = interfax.Outbound.Documents.UploadDocumentChunk(
      sessionId,
      fileStream.Position - len,
      data
    ).Result;
    // if the status code was accepted then we continue
    if (response.StatusCode == HttpStatusCode.Accepted) continue;
    // otherwise we are done and can send the fax
    if (response.StatusCode == HttpStatusCode.OK)
    {
      // we load the document so we can get its URI
      var document = await interfax.Documents.GetUploadSession(sessionId);
      // finally we send the fax using the document URI
      var faxId = await interfax.Outbound.SendFax(
          interfax.Documents.BuildFaxDocument(session.Uri),
          new SendOptions
          {
              FaxNumber = "+111111111112"
          }
      );
      break;
    }
  }
}

  

You can check on the status of this fax by following this sample code.

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