FilePond

repository·master·Indexed 12 days ago

https://github.com/pqina/filepond

A flexible JavaScript library for file uploads supporting files, directories, and URLs. Version 4.32.12 provides image optimization, i18n support, and a wide array of plugins for validation, image processing, and media previews. It includes official adapters for React, Vue, Angular, Svelte, jQuery, and others.

Tokens
7.9K
Snippets
23
Records
25
Agent score
96%

What's inside FilePond

  1. Install and set up FilePond via CDN

    master

    For simple HTML projects, you can include FilePond via unpkg. First, include the FilePond CSS stylesheet in your <head>. Then, add a file <input> element to your body. Finally, load the FilePond library and call FilePond.parse(document.body) to transform all matching file input elements into FilePond instances.

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>FilePond from CDN</title>
    
            <!-- Filepond stylesheet -->
            <link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet" />
        </head>
        <body>
            <!-- We'll transform this input into a pond -->
            <input type="file" class="filepond" />
    
            <!-- Load FilePond library -->
            <script src="https://unpkg.com/filepond/dist/filepond.js"></script>
    
            <!-- Turn all file input elements into ponds -->
            <script>
                FilePond.parse(document.body);
            </script>
        </body>
    </html>
  2. Install and set up FilePond via npm

    master

    To use FilePond in a modern JavaScript project, install the package via npm and then initialize a FilePond instance using FilePond.create(). You can pass configuration options like multiple: true to enable multi-file uploads.

    import * as FilePond from 'filepond';
    
    // Create a multi file upload component
    const pond = FilePond.create({
        multiple: true,
        name: 'filepond',
    });
    
    // Add it to the DOM
    document.body.appendChild(pond.element);
  3. Configure Internationalization (i18n) in FilePond

    master

    FilePond supports multiple languages via locale files. To change the language of the interface, import the desired locale file and apply it globally using FilePond.setOptions().

    import pt_BR from 'filepond/locale/pt-br.js';
    
    FilePond.setOptions(pt_BR);
  4. Listen to FilePond events

    master

    FilePond uses a custom event system. You can listen to events in two ways:

    1. Using the .on() method: This is the primary way to attach callbacks to specific event types.
    2. Using standard DOM events: FilePond dispatches CustomEvent objects on its root element with the prefix FilePond:. These events are configured to bubble and are composed (allowing them to cross shadow DOM boundaries).
    // Using the .on() API
    pond.on('processfile', (error, file) => {
        console.log('File processed:', file);
    });
    
    // Using standard DOM event listeners
    pond.element.addEventListener('FilePond:processfile', (e) => {
        console.log('Event detail:', e.detail);
    });
  5. Initialize FilePond with createApp()

    master

    To start using FilePond, call createApp() with an optional configuration object. This initializes the internal data store, sets up event listeners for window resizing and visibility changes, and renders the initial view. The function returns a FilePond instance containing the public API, event handlers, and DOM manipulation methods.

    import { createApp } from 'filepond/src/js/app/index';
    
    const pond = createApp({
        // your initial options here
    });
  6. Available FilePond Plugins

    master

    Extend FilePond's functionality using various plugins. Key categories include:

    Validation & Metadata

    • filepond-plugin-file-encode: File encoding
    • filepond-plugin-file-rename: File renaming
    • filepond-plugin-file-validate-size: File size validation
    • filepond-plugin-file-validate-type: File type validation
    • filepond-plugin-file-metadata: File metadata

    Image Processing

    • filepond-plugin-image-preview: Image preview
    • filepond-plugin-image-crop: Image cropping
    • filepond-plugin-image-resize: Image resizing
    • filepond-plugin-image-filter: Image filtering
    • filepond-plugin-image-transform: Image transformation
    • filepond-plugin-image-edit: Image editing (integrates with Pintura)
    • filepond-plugin-image-exif-orientation: Fixes EXIF orientation
    • filepond-plugin-image-overlay: Image overlay

    Media & Other

    • filepond-plugin-media-preview: Media preview
    • filepond-plugin-pdf-preview: PDF preview
    • filepond-plugin-zipper: Zip Directory Uploads
    • filepond-plugin-get-file: Get file
  7. Available FilePond Adapters

    master

    FilePond provides official adapters for several popular frameworks to ensure seamless integration:

    • React: @pqina/react-filepond
    • Vue: @pqina/vue-filepond
    • Angular: @pqina/ngx-filepond
    • Svelte: @pqina/svelte-filepond
    • jQuery: @pqina/jquery-filepond
    • Angular 1: angularjs-filepond
    • Blazor: soenneker.blazor.filepond
    • Ember: ember-filepond
  8. Manage global FilePond options with getOptions() and setOptions()

    master

    FilePond provides methods to manage configuration globally:

    • getOptions(): Returns the current set of default options.
    • setOptions(opts): Updates the default options for all existing FilePond instances and sets the new defaults for any future instances created. It accepts an object of configuration keys and values.
    import { getOptions, setOptions } from 'filepond';
    
    // Get current defaults
    const currentOptions = getOptions();
    
    // Update defaults for all instances
    setOptions({
        allowMultiple: true
    });
  9. Manage FilePond options and state

    master

    You can dynamically update the configuration of a running FilePond instance using setOptions(). To retrieve the current status of the pond, use the status property.

    // Update options
    pond.setOptions({
        allowMultiple: true
    });
    
    // Get current status
    const currentStatus = pond.status.get();
  10. Process and prepare files

    master

    FilePond distinguishes between 'preparing' and 'processing' files:

    • prepareFile(query): Requests the preparation of a specific file (e.g., for output generation). Returns a Promise that resolves with the prepared item.
    • processFile(query): Starts the upload/processing of a specific file. Returns a Promise.
    • prepareFiles(...args): Prepares all files or a specific subset provided as arguments.
    • processFiles(...args): Starts processing all files or a specific subset. If no arguments are provided, it attempts to process all files that are not currently idle, processing, or already completed.
    // Process a single file
    pond.processFile(fileQuery);
    
    // Process all files
    pond.processFiles();
    
    // Prepare a specific file
    pond.prepareFile(fileQuery).then(item => {
        console.log('File prepared:', item);
    });
  11. Manage FilePond global options

    master

    FilePond allows you to manage global configuration settings using getOptions() and setOptions().

    • getOptions(): Returns a copy of the current global default options.
    • setOptions(opts): Updates the global default options. Note that setOptions only updates keys that already exist in the default options object; it will not add new configuration keys. It also performs type validation based on the existing default values.
    • extendDefaultOptions(additionalOptions): Merges additional options into the existing defaults using Object.assign.
    import { getOptions, setOptions, extendDefaultOptions } from 'filepond';
    
    // Get current options
    const currentOptions = getOptions();
    
    // Update global options (only existing keys are updated)
    setOptions({
        allowMultiple: true,
        labelIdle: 'Drop files here'
    });
    
    // Extend defaults
    extendDefaultOptions({
        someNewKey: 'value'
    });
  12. Execute a chain of filters with applyFilterChain()

    master

    The applyFilterChain(key, value, utils) function executes all registered filters associated with a specific key in a sequential pipeline.

    Each filter in the chain receives the output of the previous filter as its input. This is useful for complex, multi-step transformations where each step depends on the result of the last. The function returns a Promise that resolves with the final transformed value or rejects if any filter in the chain fails.

    import { applyFilterChain } from 'filepond';
    
    // Usage within the FilePond lifecycle
    applyFilterChain('some-key', initialValue, utils)
        .then(finalValue => {
            console.log('Transformation complete:', finalValue);
        })
        .catch(error => {
            console.error('Filter chain failed:', error);
        });