0.3.1 • Published 3 years ago

@olenbetong/file-upload v0.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

FileUploader

A simple class used to upload File or Blob objects to Appframe data sources.

Install

$ npm install @olenbetong/file-upload

Usage

The constructor takes an options parameter with 4 possible options:

  • articleId - ID of the article where the data object is (will use af.article.id if not set)
  • dataObject - The data object to upload to
  • fieldName (optional) - Field name (for binary fields?)
  • primKey (optional) - Use to upload to an existing record(?)

To get status updates, use the attachEvent and detachEvent methods to subscribe to the onProgress event. The event handler will be called with an object with loaded and total properties. When the upload is finished, an onUploaded event will be triggered. If the upload fails, an onUploadFailed event will be triggered. The upload method returns a promise that can be used instead of the onUploaded and onUploadFailed events.

The uploadmethod takes 3 parameters:

  • fileOrBlob - The file or blob object to upload
  • fields (optional) - Additional fields that should be set on the record (linked fields from master objects are automatically added)
  • fileName (optional) - Name of the file. Only used for blob objects.
let uploader = new FileUploader({ dataObject });
uploader.attachEvent("onProgress", updateProgress);

function updateProgress({ loaded, total }) {
  document.querySelector("#progress").innerText = `${loaded}/${total} uploaded`;
}

let files = input.files;
for (let file of files) {
  try {
    await uploader.upload(file, { RelatedID: 42 });
  } catch (error) {
    alert(error.toString());
  }
}