Node.JS Fax - Send a fax with a large file

This is a more advanced Node.JS code sample to show how to send a fax with a large document. This code sample is based on the Node.JS 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.

  
     var fs = require('fs');

fs.readFile('tests/test.pdf', uploadAndSend);

// upload and then send the document
function uploadAndSend(error, data){
  interfax.documents.create(
    'test.pdf',
    Buffer.byteLength(data)
  ).then(function(document){
    upload(0, document, data);
  });
}

// upload the file in 500 byte chunks
function upload(cursor = 0, document, data) {
  if (cursor >= data.length) {
    sendFax(document);
  }

  var chunk       = data.slice(cursor, cursor+500);
  var next_cursor = cursor+Buffer.byteLength(chunk);

  interfax.documents.upload(
    document.id,
    cursor,
    next_cursor-1,
    chunk
  ).then(function() {
    upload(next_cursor, document, data);
  });
}

// send the fax
function sendFax(document) {
  interfax.outbound.deliver({
    // a valid fax number
    faxNumber: "+11111111112",
    // the document URI
    file: document.uri
  });
}

  

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