1.1.1 • Published 3 years ago

filestack-drag-and-drop-js v1.1.1

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

Filestack Drag And Drop

With Filestack-Drag-and-Drop you can easily add drag-and-drop file uploading support to your website. You only need 3 lines of code to make any element on your page able to do that. Filestack-drag-and-drop is a frontend to our JavaScript SDK library.

Getting Started

UMD module

To integrate FIlestack-Drag-and-Drop with your web application simply include our UMD module in your code:

<script src="//static.filestackapi.com/filestack-drag-and-drop-js/{MAJOR_VERSION}.x.x/filestack-drag-and-drop.min.js"></script>

Add an element to your page:

<div class="drop-container">Drag and Drop</div>

and initialize Filestack Drag and Drop:

const filestackDnD = new filestackDnD.init('API_KEY', document.querySelector('.drop-container'));

That's it. Now your page element handles the upload by dropping a file on it.

Example initialization with filestack Client:

const filestackClient = filestack.init('API_KEY');
const filestackDnD = new filestackDnD.init(filestackClient, document.querySelector('.drop-container'));

Example initialization with Options (only image, max size: 1024, max files: 2):

const config = {
  accept: ['image/*'], // default empty array - all files
  maxSize: 1024, // default 0 - no limit
  maxFiles: 2, // default 0 - no limit
  failOverMaxFiles: false, 
}

const filestackDnD = new filestackDnD.init('API_KEY', document.querySelector('.drop-container'), config);

Example initialization with sdkConfig:

const sdkConfig = {
  cname: 'cname_test',
  security: {
      policy : 'policy_test',
      signature: 'signature_test'
  }
}

const filestackDnD = new filestackDnD.init('API_KEY', document.querySelector('.drop-container'), null, null, testConfig);

SRI

Subresource Integrity (SRI) is a security feature that enables browsers to verify that files they fetch (for example, from a CDN) are delivered without unexpected manipulation. It works by allowing you to provide a cryptographic hash that a fetched file must match

To obtain sri hashes for filestack-tools library check manifest.json file on CDN:

https://static.filestackapi.com/filestack-drag-and-drop-js/{LIBRARY_VERSION}/manifest.json
<script src="//static.filestackapi.com/filestack-drag-and-drop-js/{LIBRARY_VERSION}/filestack-drag-and-drop.min.js" integrity="{FILE_HASH}" crossorigin="anonymous"></script>

Where {LIBRARY_VERSION} is currently used library version and {FILE_HASH} is one of the hashes from integrity field in manifest.json file

Objects:

FilestackDnD

Constructor

constructor(apikey: string | Client, element?: HTMLElement, options?: OptionsInterface, sdkConfig?: ClientOptions)
NameTypeDescription
apikeystring | ClientApplication ApiKey or instance Client from filstackSDK
element (optional)HTMElementThe HTML element that should listen to events
options (optional)OptionsInterfaceSettings related to uploading
sdkConfig (optional)ClientSettings for SDK

Properties

NameTypeDescription
elementsHelperElementHelperManages elements
eventEmitterHelperEventEmitterManages events
uploadsHelperUploadHelperManages uploading
filstackSdkClientClient form filstackSDK

Example: Use elementsHelper - set new HTMLElement:

filestackDnD.elementHelper.setElement(document.querySelector('.someElement'))

Use eventEmitterHelper - listen to events:

filestackDnD.eventEmitterHelper.on('dragover', (res) => {console.log(e)})

Use uploadsHelper - set Upload Options:

filestackDnD.uploadsHelper.setUploadOptions()

Use filstackSdk - open picker:

filestackDnD.filstackSdk.picker(options).open();

Methods

NameParametersDescriptionReturn
setElementselements: HTMLElement | HTMLElement[] | NodeListOfAdds elements to support Drag and Dropvoid
setUploadOptionsuploadOptions: UploadOptionsAdds an option to upload filesvoid
onevent: string, ...args: any[]Listening to events. List of available eventsvoid
emitevent: string, listener: ListenerSending events. List of available eventsvoid

ElementHelper

Properties

NameTypeDescription
elementsHTMLElement[]Manages elements

Methods

NameParametersDescriptionReturn
setElementelement: HTMLElementAdding an item. Previous elements are overwrittenvoid
setElementselements: HTMLElement[] | NodeListOfAdding items. The previous ones are overwrittenvoid
addElementelement: HTMLElementAdding element to the listvoid
addElementselements: HTMLElement[] | NodeListOfAdding items to the listvoid

UploadHelper

Methods

NameParametersDescriptionReturn
setUploadOptionsuploadOptions: UploadOptionsSet upload Optionsvoid
setStoreUploadOptionsstoreUploadOptions: StoreUploadOptionsSet store upload optionsvoid
setSecuritysecurity: Securityset security configvoid

Events

You can interact programmatically with Filestack Drag and Drop

How to listen to events

filestackDnD.on('eventName', (e) => {console.log(e)});
NameDescriptionReturn
successReadFileReading files through browsersSuccessReadFileInterface
progressChecking the progressProgressInterface
dragoverEvent triggered when an object is dragged over an element added to Drag and Drop{ elementId: string, data: DragEvent }
dragleaveEvent triggered when the user moves the cursor outside a Drag and Drop supported item{ elementId: string, data: DragEvent }
dropEvent triggered when user drops a file over an item{ elementId: string, data: DragEvent }
uploadFileFinishEvent triggered when uploading a file is successful{ elementId: string, files: NormalizeFileInterface[], data: res }
errorEvent triggered when there are some errors e.g. wrong file format, problems with uploading etcEventInterface

How to send events

filestackDnD.emit('eventName', { elementId: null, fileId: id });
  • elementId: string - This is the identifier of the HTML element. A unique attribute is added: "data-fs-dnd-element-id" To every element added to DragAndDrop.
  • fileId: string - Each file has its own unique Id.

If we send an elementId to some event, the event will be called for all files added to this element. If we send only fileId the event will be fired only for the specific file

NameDescription
pauseStops the upload
resumeResumes the upload
cancelAborts file upload

Examples of usage

Take a look at the examples folder as well. We show various use cases there (example_simple.html):

Multiple drag and drop elements on the page (example_multi_pane.html):

List of uploaded files with progress (example_file_lists.html):

Contributing

We follow the conventional commits specification to ensure consistent commit messages and changelog formatting.

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.0

3 years ago

1.0.1

3 years ago